简体   繁体   中英

How to hide a view of recycler view in a single fragment in android (But using same list in two fragments)

I am using a recycler view in two fragments.

But one of the fragments needs not to show a particular view. But Need to show all the views in another screen.

How can I hide that particular view alone in one of my fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:background="@drawable/corner_radius"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="left"
            android:foregroundGravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:textColor="@color/white_new"
            android:textSize="20dp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="left"
            android:foregroundGravity="center_horizontal"
            android:layout_marginLeft="10dp"
            android:textColor="@color/gray"
            android:textSize="18dp"/>

    </LinearLayout>
</LinearLayout>
    <View style="@style/Divider.Horizontal.Gray" android:id="@+id/divider"  android:layout_marginTop="10dp" android:layout_marginBottom="10dp"/>
</LinearLayout>

Why not you use if condition for this or

yourAdapter(isoneFramgent:Boolean){

}

send true and false from your fragment if its from first fragment send true and its from second fragment send false and add a if condition into bindView to show and hide your view according to fragment

Filter your data source before setting it to adapter and pass the filtered data to one fragment and raw data to other fragment.

Suppose you have list of strings

val originalData = listOf("data") //this is your raw list
//Fragment 1
  val f1Data = ArrayList(originalData.filter{ //filter one item })
  val f1Adapter = YourAdapter(f1Data)
//Fragment 2
  val f2Adapter = YourAdapter(originalData)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM