Ich manuell Navigationsablage mit ActionBarDrawerToggle
und DrawerLayout
gemacht, jetzt möchte ich ausgewählte Element hervorgehoben bleiben, entweder wenn ich die Schublade öffnen oder schließen Sie das ausgewählte Element (Fragment) sollte mit einer Farbe hervorgehoben werden . Ich habe eine ListView
in meiner Schublade. Dies ist drawer.xml (Fragment)Bleiben Sie markiert das ausgewählte Element in der Navigationsleiste
<?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="match_parent"
android:background="#34344d"
android:orientation="vertical" >
<ListView
android:id="@+id/drawerlist_1"
android:dividerHeight="0dp"
android:divider="#fffff7"
android:layout_weight="1"
android:listSelector="@drawable/list_view_scolor"
android:layout_width="fill_parent"
android:layout_height="0dp" >
</ListView>
</LinearLayout>
I listSelector-Datei, die list_view_scolor ist:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item
android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/default_color" />
<item
android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/default_color" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/list_view_listselector" />
<item
android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/list_view_listselector" />
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/list_view_listselector" />
</selector>
I
haben list_view_listselector.xml<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/pressed_color" />
</shape>
Ich habe nicht geschrieben Code von SINGLE_ROW , wenn Sie diese Datei sehen müssen, kann ich das aktualisieren. Bitte kommentieren Sie bei Bedarf.
BaseAdapter für Listenansicht: -
public class ListView_Adapter extends BaseAdapter {
Context context;
String[] list;
LayoutInflater inflater;
public ListView_Adapter(Context context , String[] list) {
this.context = context;
this.list = list;
inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
view= inflater.inflate(R.layout.cust_row_listview, null);
TextView drawer_item = (TextView) view.findViewById(R.id.drawer_item);
String item = list[position];
drawer_item.setText(item);
Typeface type = Typeface.createFromAsset(context.getAssets(),
"robot_condensed_light.ttf");
drawer_item.setTypeface(type);
/* if(position==0){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_1)));
}
else if(position == 1){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_2)));
}
else if(position == 2){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_3)));
}
else if(position == 3){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_4)));
}
else if(position == 4){
view.setBackgroundColor(Color.parseColor(context.getResources().getString(R.string.color_list_5)));
}*/
return view;
}
}
Dies ist Implementierung, wie ich navigieren Sie durch Fragmente
lv = (ListView) findViewById(R.id.drawerlist_1);
ListView_Adapter adapter1 = new ListView_Adapter(this, item1);
lv.setAdapter(adapter1);
Fragment_Home frag = new Fragment_Home();
fragManager = getSupportFragmentManager();
fragManager.beginTransaction().replace(R.id.frameLayout, frag).commit();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//lv.setItemChecked(position, true);
//v.setSelection(position);
lv.setSelected(true);
Fragment frag;
if (position == 0) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(getResources().getColor(R.string.color_list_2)));
getSupportActionBar().setTitle("Home");
frag = new Fragment_Home();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
} else if (position == 1) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_2))));
frag = new Fragment_PaytmWallet();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("PayTM Wallet");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_2)));
// }
} else if (position == 2) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_3))));
frag = new Fragment_Categories();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Categories");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_3)));
// }
} else if (position == 3) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_4))));
frag = new Fragment_AskAQues();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Ask a Question");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_4)));
// }
} else if (position == 4) {
// getSupportActionBar().setBackgroundDrawable(new
// ColorDrawable(Color.parseColor(getResources().getString(R.string.color_list_5))));
frag = new Fragment_BeAnExpert();
fragManager.beginTransaction()
.replace(R.id.frameLayout, frag).commit();
getSupportActionBar().setTitle("Be an Expert");
// if(Build.VERSION.SDK_INT>=21){
// Window window = getWindow();
// window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// window.setStatusBarColor(Color.parseColor(getResources().getString(R.string.color_list_primary_5)));
// }
}
dl.closeDrawers();
}
});
können Sie teilen, wie Sie Ihre Fragmente in die Navigationsleiste laden? –
Mit FragmentManager können Sie Fälle erstellen, indem Sie auf "Listenansicht" klicken und Fragmente und dann FragmentManager instanziieren. Soll ich Code hochladen? – user6556461
mit Hilfe von Adapter rechts? –