Recyler视图项仅获取上单击文本选择 [英] Recyler view item gets selected only on clicking the text

查看:266
本文介绍了Recyler视图项仅获取上单击文本选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了RecyclerView与项目适配器和每个项目都有包含的ImageView和一个TextView抽屉的项目布局。结果
问题是,被点击仅当图像或不同的大小的实际文本,而不是整个行的onClick函数。结果,
有没有一种方法,使全行的onClick与功能听呢?请让我知道,如果你有任何想法。
先谢谢了。结果

I have implemented the RecyclerView with an adapter for items and each item has a drawer item layout containing an imageview and a textview.
The problem is that the onClick function is called only when the image or the "actual text" which differs in size is clicked and not the whole row.
Is there a way to make to whole row listen with onClick function? Please let me know if you have any ideas. Thanks in advance.
RecyclerView image

RecyclerViewAdapter.java

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

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中

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>

请让我知道如果你需要更多的code。

Please let me know if you need more code.

推荐答案

在适配器的 onCreateViewHolder()方法,将膨胀()调用如下:

In the Adapter's onCreateViewHolder() method, change the inflate() calls as follows.

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 的宽度,因为你不及格来的充气器,所以它的包装纸宽度来代替。

Your rows aren't matching the RecyclerView's width, because you're not passing parent to the Inflater, so it's wrapping the width instead.

这篇关于Recyler视图项仅获取上单击文本选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆