我知道这个问题已经重复了很多次,但是我尝试了各种方法,但仍然没有成功获得理想的结果。
我只想让所选项目的背景在用户点击时获得更改。
我使用的代码是:
RecyclerViewAdapter.java
`public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){
LayoutInflater layoutInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(viewType==1){
View itemLayout = layoutInflater.inflate(R.layout.drawer_item_layout,null);
return new ViewHolder(itemLayout,viewType,context);
}
else if (viewType==0) {
View itemHeader = layoutInflater.inflate(R.layout.header_layout,null);
@Override
public void onBindViewHolder(RecyclerViewAdapter.ViewHolder holder, int position) {
if(position!=0){
holder.navTitle.setText(titles[position - 1]);
holder.navIcon.setImageResource(icons.getResourceId(position-1,-1));
}
}
@Override
public int getItemCount() {
return titles.length+1;
}
@Override
public int getItemViewType(int position) {
if(position==0)return 0;
else return 1;
}
}
<br><br>
activity_main.xml
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/containerView">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
android:id="@+id/toolBar"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark"
/>
</FrameLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="250dp"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
android:scrollbars="vertical"
android:background="#FFFFFF"
android:layout_gravity="left"
android:fitsSystemWindows="true"
/>
<br><br>
MainActivity.java
公共类MainActivity扩展了AppCompatActivity {
Toolbar toolbar;
public DrawerLayout drawerLayout;
RecyclerView recyclerView;
String navTitles[];
TypedArray navIcons;
RecyclerView.Adapter recyclerViewAdapter;
ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Let's first set up toolbar
setupToolbar();
//Initialize Views
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);
//Setup Titles and Icons of Navigation Drawer
navTitles = getResources().getStringArray(R.array.navDrawerItems);
navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);
recyclerViewAdapter = new RecyclerViewAdapter(navTitles,navIcons,this);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
setupDrawerToggle();
Fragment FirstFragment = new FirstFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.containerView, FirstFragment, null);
fragmentTransaction.commit();
}
void setupToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
void setupDrawerToggle(){
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
//This is necessary to change the icon of the Drawer Toggle upon state change.
drawerToggle.syncState();
}
}`