只有在单击文本时才会选择Recyler视图项

时间:2016-01-24 17:56:37

标签: android android-layout drawerlayout recycler-adapter actionbardrawertoggle

我已经使用适配器实现了RecyclerView项目,每个项目都有一个包含imageview和textview的抽屉项目布局。
问题是仅当单击图像或大小不同的“实际文本”而不​​是整行时才调用onClick函数。
有没有办法用onClick函数进行整行监听?如果您有任何想法,请告诉我。 提前谢谢。
RecyclerView image

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
String[] titles;
TypedArray icons;
Context context;
RecyclerViewAdapter(String[] titles, TypedArray icons, Context context){

        this.titles = titles;
        this.icons = icons;
        this.context = context;
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener  {

    TextView navTitle;
    ImageView navIcon;
    Context context;

    public ViewHolder(View drawerItem , int itemType , Context context){

        super(drawerItem);
        this.context = context;
        drawerItem.setOnClickListener(this);
        if(itemType==1){
            navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle);
            navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
        }
    }

   /**
    *This defines onClick for every item with respect to its position.
    */

    @Override
    public void onClick(View v) {

        MainActivity mainActivity = (MainActivity)context;
        mainActivity.drawerLayout.closeDrawers();
        FragmentTransaction fragmentTransaction = mainActivity.getSupportFragmentManager().beginTransaction();

        //switch (getPosition()){
        switch (getAdapterPosition()){
            case 1:
                Fragment fragment = new CountryWise();
                fragmentTransaction.replace(R.id.containerView,fragment);
                fragmentTransaction.commit();
                break;
            case 2:
                Fragment f2 = new CompanyWise();
                fragmentTransaction.replace(R.id.containerView,f2);
                fragmentTransaction.commit();
                break;
            case 3:
                Fragment about = new About();
                fragmentTransaction.replace(R.id.containerView,about);
                fragmentTransaction.commit();
                break;
        }
    }
}

@Override
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);
            return new ViewHolder(itemHeader,viewType,context);
        }

    return null;
}

   /**
*This method is called by RecyclerView.Adapter to display the data at the specified position.�
*This method should update the contents of the itemView to reflect the item at the given position.
*So here , if position!=0 it implies its a row_item and we set the title and icon of the view.
*/

@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 getItemViewType(int position) {
    if(position==0)return 0;
    else return 1;
}

}

drawer_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:id="@+id/iv_NavIcon"
    />

<TextView
    android:layout_gravity="center"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:id="@+id/tv_NavTitle"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#000000"
    android:textSize="18sp"
    android:textStyle="bold"
    />

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        android:minHeight="?android:attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark" />

    <FrameLayout
        android:id="@+id/containerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    </FrameLayout>
</LinearLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#FFFFFF"
    android:paddingTop="40dp"
    android:scrollbars="vertical" />

</android.support.v4.widget.DrawerLayout>

如果您需要更多代码,请与我们联系。

2 个答案:

答案 0 :(得分:1)

在适配器的onCreateViewHolder()方法中,按如下方式更改inflate()来电。

if(viewType == 1) {
    View itemLayout = layoutInflater.inflate(R.layout.drawer_item_layout, parent, false);
    return new ViewHolder(itemLayout, viewType, context);
}
else if(viewType == 0) {
    View itemHeader = layoutInflater.inflate(R.layout.header_layout, parent, false);
    return new ViewHolder(itemHeader, viewType, context);
}

您的行与RecyclerView的宽度不匹配,因为您没有将parent传递给Inflater,因此它会包裹宽度代替。

答案 1 :(得分:0)

你可以试试这个 在您的情况下将线性布局设置为RecyclerView的父布局,并在View支架中绑定此布局,并将onclick设置为此布局,它应该可以使用