Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...