Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
158 views
in Technique[技术] by (71.8m points)

android - How to Hide the row item of Recycleview from fragment

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...