statt Ansicht Verwendung Recycler Menü, so dass Sie viel mehr Kontrolle über die Elemente. Der folgende Code ist getestet und funktioniert.
Dies ist löschte:
Schritt eins: Artikel Layouts erstellen:
item_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/image"
android:layout_marginLeft="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="16dp"
android:text="Title" />
</LinearLayout>
nichts Besonderes, einfach nur Bild und ein Text
Schritt 2 : Für unser Toggle-Layout können wir item_toggle.xml
erstellen
item_toggle.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textCustom"
android:layout_marginLeft="16dp"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notifications" />
<ToggleButton
android:layout_marginRight="16dp"
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="New ToggleButton" />
</RelativeLayout>
Schritt 3: Damit können Adapter für unsere Recycler Ansicht erstellen, die in unserer Navigationsansicht
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter {
String Tag = CustomAdapter.class.getSimpleName();
private final int NORMAL_VIEW = 88; // code for normal view type
private final int CUSTOM_VIEW = 99; //code for loader type
private Context context;
private ArrayList<Model> listingData = new ArrayList<>();
public CustomAdapter(Context context) {
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
if (viewType == NORMAL_VIEW) {
View normalView = inflater.inflate(R.layout.item_normal, parent, false);
return new NormalItem(normalView);
} else if (viewType == CUSTOM_VIEW) {
View customView = inflater.inflate(R.layout.item_toogle, parent, false);
return new CustomItem(customView);
} else return null; // unknown type.
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof NormalItem) {
// normal list item.
((NormalItem) holder).textView.setText(listingData.get(position).getName());
((NormalItem) holder).icon.setImageResource(R.mipmap.ic_launcher);
} else if (holder instanceof CustomItem)
{
((CustomItem) holder).textViewToggle.setText("On");
((CustomItem) holder).textViewToggle.setText("Notifications");
}
}
@Override
public int getItemCount() {
return listingData.size();
}
@Override
public int getItemViewType(int position) {
if (position == 3) return CUSTOM_VIEW;
else return NORMAL_VIEW;
}
public class NormalItem extends RecyclerView.ViewHolder {
ImageView icon;
TextView textView;
public NormalItem(View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.image);
textView = (TextView) itemView.findViewById(R.id.text);
}
}
public class CustomItem extends RecyclerView.ViewHolder {
ToggleButton toggleButton;
TextView textViewToggle;
public CustomItem(View itemView) {
super(itemView);
toggleButton = (ToggleButton) itemView.findViewById(R.id.toggleButton);
textViewToggle = (TextView) itemView.findViewById(R.id.textCustom);
}
}
/**
* method to update the adapter and notify the adapter item by item
*
* @param data data to be updated.
*/
public void updateAdapter(ArrayList<Model> data) {
try {
for (Model item : data) {
listingData.add(item);
notifyItemInserted(getItemCount() - 1);
}
} catch (Exception e) {
Log.e(Tag, e.toString() + "");
}
}
}
Schritt 4 sein wird: ein erstellen activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".main.MainActivity">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="16dp"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Schritt 5: Ermöglicht eine einfache Klasse erstellen
Model.java unsere Daten
public class Model {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
String name;
int item;
}
Schritt 6 zu halten:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/android"
app:id="@+id/toolbar"
android:elevation="4dp"
app:layout_width="match_parent"
app:layout_height="wrap_content"
app:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimary"
/>
letzter Schritt: Erstellen MainActiivty .klasse
Hauptaktivität.Klasse
public class MainActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private RecyclerView recyclerView;
private ArrayList<Model> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Custom Nav ");
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(mToogle);
mToogle.syncState();
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(manager);
CustomAdapter adapter = new CustomAdapter(this);
recyclerView.setAdapter(adapter);
data = new ArrayList<>();
Model item1 = new Model();
item1.setName("Option 1");
item1.setItem(R.mipmap.ic_launcher);
Model item2 = new Model();
item2.setName("Option 2");
item2.setItem(R.mipmap.ic_launcher);
Model item3 = new Model();
item3.setItem(R.mipmap.ic_launcher);
item3.setName("Option 3");
Model item4 = new Model();
item4.setItem(R.mipmap.ic_launcher);
item4.setName("Option 4");
data.add(item1);
data.add(item2);
data.add(item3);
data.add(item4);
adapter.updateAdapter(data);
}
}
Sie können Header hinzufügen und die Benutzeroberfläche nach Ihren Bedürfnissen polieren. Ich hoffe es hilft. Glückliche Codierung.
NavigationView ist für die Navigation innerhalb und App, so würde ich nicht erwarten, dass es für das Umschalten alles verwenden. Es ist jedoch üblich, Aktionsleistenmenüelemente als überprüfbare Umschaltelemente zu verwenden, die mehr Sinn ergeben. –
@DougStevenson Ich habe gerade die Frage aktualisiert. Könnten Sie bitte das Bild noch einmal überprüfen, damit ich sicher sein kann, dass das kein guter Weg ist. –
Wenn Sie mit dieser Implementierung zufrieden sind, dann gehen Sie auf jeden Fall mit. Es scheint unkonventionell, aber vielleicht ist das egal? –