如何从片段中隐藏Recycleview的行项目[英] How to Hide the row item of Recycleview from fragment

本文是小编为大家收集整理的关于如何从片段中隐藏Recycleview的行项目的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个片段类,该类使用reccleview来显示文本和图像列表. 我如何访问RecycleView行项目并将自定义文本设置为任何文本视图,或从Fragment类中隐藏所需的TextView.

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingBottom="4dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" />

  <include
    android:id="@+id/include_tag"
    layout="@layout/row_item"/>
 </FrameLayout>

我的行项目布局是

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res   /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent">

    <TextView
        android:id="@+id/flower_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:maxLines="1"
        android:layout_centerInParent="true"
        android:gravity="center_vertical|center_horizontal"
        android:textAlignment="center"
        android:textColor="#E91E63"
        android:fontFamily="sans-serif-condensed"
        android:text="Sagar Rawal"/>

            <TextView
                android:layout_marginTop="8dp"
                android:id="@+id/flower_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:maxLines="3"
                android:textColor="#673AB7"
                android:text="This Text need to Be Change "/>



</RelativeLayout>

和我的片段级i s

 public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
   final View view = inflater.inflate(R.layout.activity_main, container, false);



    View test1View = view.findViewById(R.id.include_tag);
    mhideText = (TextView) test1View.findViewById(R.id.flower_details);
    mhideText.setVisibility(View.INVISIBLE);

return view;

但行不通.和文本视图仍然可见.

请帮助.预先感谢.

推荐答案

您必须有一个自定义视图持有人,该自定义视图持有人应扩展RecyClerview.ViewHolder.在您的视图持有人中,您可以链接行项目,并且可以从行布局中访问所有视图. 您的自定义视图持有人看起来像:

public static class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textView;
    public MyViewHolder(TextView v) {
        super(v);
        textView = v;
        //Note: To set custom text
        textView.setText("My custom text");

        //Note: To hide TextView remove comment from below line
        //textView.setVisibility(View.GONE);  
    }
}

上面的代码将示例视图持有人显示为您可以访问视图的MyViewholder. 如果您将父级布局传递给myviewholder而不是传递textView,则可以使用v.findviewbyid(r.id.xxx)

找到子视图.

您的适配器应包含类似的代码:

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    ...
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

在上面的代码段中,通过传递视图 textview 来创建myviewholder实例.您可以通过您的行布局中的Relativelayout 传递父级布局.

使用此视图持有人在视图上采取任何操作,例如将自定义文本设置为TextView,或者您可以根据要求隐藏TextView.

参考完整示例,您可以按照Android文档或单击在这里.

要在视图持有人和片段之间实现通信,您可以使用接口.

谢谢!

其他推荐答案

尝试这样做:

rowview = inflater.inflate(R.layout.row_item, container, false);
mhideText = (TextView)rowview.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);

本文地址:https://www.itbaoku.cn/post/2091087.html

问题描述

I have a fragment class which uses Recycleview to show list of Text and Image. How can I access Recycleview row items and set an custom text to any Textview or Hide the Desired Textview from fragment class.

This is my layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:paddingBottom="4dp"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:paddingTop="4dp">
<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" />

  <include
    android:id="@+id/include_tag"
    layout="@layout/row_item"/>
 </FrameLayout>

and My row item layout is

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res   /android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent">

    <TextView
        android:id="@+id/flower_Name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textStyle="bold"
        android:maxLines="1"
        android:layout_centerInParent="true"
        android:gravity="center_vertical|center_horizontal"
        android:textAlignment="center"
        android:textColor="#E91E63"
        android:fontFamily="sans-serif-condensed"
        android:text="Sagar Rawal"/>

            <TextView
                android:layout_marginTop="8dp"
                android:id="@+id/flower_details"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="14sp"
                android:textStyle="bold"
                android:maxLines="3"
                android:textColor="#673AB7"
                android:text="This Text need to Be Change "/>



</RelativeLayout>

and my Fragment class i s

 public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
   final View view = inflater.inflate(R.layout.activity_main, container, false);



    View test1View = view.findViewById(R.id.include_tag);
    mhideText = (TextView) test1View.findViewById(R.id.flower_details);
    mhideText.setVisibility(View.INVISIBLE);

return view;

But it doesn't work. and Textview is still Visible.

Please help. Thanks in advance.

推荐答案

You must have a custom ViewHolder which should extends RecyclerView.ViewHolder. In your ViewHolder you can link row item and you can access all views from your row layout. Your custom ViewHolder looks like:

public static class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textView;
    public MyViewHolder(TextView v) {
        super(v);
        textView = v;
        //Note: To set custom text
        textView.setText("My custom text");

        //Note: To hide TextView remove comment from below line
        //textView.setVisibility(View.GONE);  
    }
}

Above code shows sample ViewHolder as MyViewHolder where you can access your views. If you are passing parent Layout to MyViewHolder instead of passing TextView then you can find child views using v.findViewById(R.id.xxx)

Your adapter should contain similar code as below:

@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    ...
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

In above code snippet, creating instance of MyViewHolder by passing the view TextView. You can pass parent layout in your case its RelativeLayout from your row layout.

Use this ViewHolder to take any actions on views like set custom text to TextView or you can hide TextView as per your requirements.

To refer full example you can follow the android documentation or click here.

To achieve communication between ViewHolder and fragment you can use Interface.

Thank you!

其他推荐答案

Try doing this :

rowview = inflater.inflate(R.layout.row_item, container, false);
mhideText = (TextView)rowview.findViewById(R.id.flower_details);
mhideText.setVisibility(View.INVISIBLE);