2017-03-24 5 views
0

Ich werde Gridview mit einer Schaltfläche als letzte Element von Gridview erstellen, die ich als Add-Schaltfläche verwenden möchte.Gridview mit Hinzufügen-Taste

Zum Beispiel: Wenn Sie auf die Schaltfläche klicken (Punkt 1) in den gridview und (Schaltfläche Hinzufügen) gehen zu Second Artikel


element1 Knopf

element1 Element2 Item3 Item4 Knopf


Etwas wie Top-Stil

+0

prüfen dieses [Tutorial] (http://chintanrathod.com/recyclerview-android-studio-part-1/) –

Antwort

0

Eine Möglichkeit besteht darin, dass Sie mehrere Rasterelementlayouts gemäß Ihren Anforderungen entwerfen können. Und dann können Sie in der "getView" -Methode der Adapter-Klasse überprüfen, welches xml als Ansicht aufgepumpt werden soll.

hoffe, das wird Ihnen helfen, es auszugraben.

0

Hier haben Sie, was ich getan habe, um dies zu erreichen. MainActivity Klasse:

public class MainActivity extends AppCompatActivity { 

GridView gridView; 
GridAdapter adapter; 
List<String> list; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    list = new ArrayList<>(); 
    for (int i = 0; i < 5; i++) { 
     list.add("Test " + i); 
    } 
    gridView = (GridView) findViewById(R.id.gridView); 
    adapter = new GridAdapter(this, list); 
    gridView.setAdapter(adapter); 
} 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.stackoverflow.MainActivity"> 

<GridView 
    android:id="@+id/gridView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</GridView> 
</RelativeLayout> 

nun einen Adapter für Gridview machen

public class GridAdapter extends BaseAdapter { 

List<String> list; 
Context context; 

public GridAdapter(Context context, List<String> list) { 
    this.list = list; 
    this.context = context; 
} 

@Override 
public int getCount() { 
    return list.size(); 
} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    if (view == null) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.griditem, parent, false); 
    } 
    TextView textView = (TextView) view.findViewById(R.id.textView); 
    LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout); 
    textView.setText(list.get(position)); 
    if (list.size() - 1 == position) { 
     linearLayout.addView(addButton()); 
    } 
    return view; 
} 

private Button addButton() { 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT); 
    Button button = new Button(context); 
    button.setLayoutParams(params); 
    button.setText("Testing"); 
    return button; 
} 

} 

Und gridItem.xml ist

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/linearLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<TextView 
    android:id="@+id/textView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_horizontal" 
    android:text="Testing" 
    android:textAppearance="@style/TextAppearance.AppCompat.Headline" 
    android:textStyle="bold" /> 

</LinearLayout> 

Es ist das wichtigste Idee t Hast du den letzten Gegenstand und füge endlich einen Knopf hinzu. Sie können diesen Code verfeinern und auch recyclerView verwenden. Wenn Sie nun ein Element zur Liste hinzufügen, rufen Sie notifyDataSetChanged() auf; Hoffe, es wird dir helfen.

enter image description here