在Scrollview中使用Recyclerview扩展Listview

时间:2018-05-02 12:06:09

标签: android android-recyclerview collapse expand android-nestedscrollview

我在下面的代码中尝试使用recyclerview(listview)a link

实现扩展和折叠内容
final boolean isExpanded = position==mExpandedPosition;
holder.details.setVisibility(isExpanded?View.VISIBLE:View.GONE);
holder.itemView.setActivated(isExpanded);

if (isExpanded)
   previousExpandedPosition = position;

holder.itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        mExpandedPosition = isExpanded ? -1:position;
        notifyItemChanged(previousExpandedPosition);
        notifyItemChanged(position);
    }
});

在我的情况下,当我点击recyclerview中的特定位置时它的扩展但它超过了recyclerview.I无法看到完整的扩展内容。我只能看到部分内容。在这种情况下我需要滚动recyclerview来查看完整的内容。但我正在寻找一个解决方案来查看内容,而无需滚动recyclerview。如果我点击recyclerview中另一个应该放在recyclerview上的位置。

**SCREENSHOT**

听到我的代码

public class CommonFragment extends Fragment {

@BindView(R.id.news_lists)
RecyclerView news_lists;
@BindView(R.id.nested_scroll)
NestedScrollView nested_scroll;
ArrayList<String> Names;
ArrayList<String> responseProducts = null;
NewsListAdaspters mAdapter;
Context mContext;
String position_name;


public CommonFragment() {
}

public static CommonFragment getInstance(String position, ArrayList<String> response) {
    CommonFragment fragmentDummy = new CommonFragment();
    Bundle args = new Bundle();
    args.putStringArrayList("Types", response);
    args.putString("position", position);
    fragmentDummy.setArguments(args);
    return fragmentDummy;
}

@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    this.responseProducts = args.getStringArrayList("Types");
    this.position_name = args.getString("position");

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}


View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.fragment_common, container, false);
        ButterKnife.bind(this, view);
    } catch (InflateException ignored) {
    }

    mContext = getActivity();

    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
    news_lists.setLayoutManager(mLayoutManager);
    news_lists.setNestedScrollingEnabled(false);
    Names = new ArrayList<>();
    Names.clear();
    for (int i = 0; i < 15; i++) {
        Names.add("News Details " + i);
    }
    Log.e("position_name==>", "" + position_name);

    getList();
    return view;
}

private void getList() {
    if (responseProducts.size() > 0) {
        mAdapter = new NewsListAdaspters(mContext, responseProducts, position_name);
        news_lists.setAdapter(mAdapter);
    }
}


public class NewsListAdaspters extends RecyclerView.Adapter<NewsListAdaspters.MyViewHolder> {
    private ArrayList<String> data;
    private Context context;
    private String name;
    int mExpandedPosition = -1;
    int previousExpandedPosition = -1;

    NewsListAdaspters(Context context, ArrayList<String> maps, String selectedFragmentName) {
        this.context = context;
        this.data = maps;
        this.name = selectedFragmentName;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.common_view, parent, false);
        final MyViewHolder holder = new MyViewHolder(itemView);

        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.news_description.setText(data.get(position));
        holder.news_title.setText(name);


        final boolean isExpanded = position == mExpandedPosition;
        Log.e("isExpanded=====>", "" + isExpanded);
        holder.expend_layout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
        holder.itemView.setActivated(isExpanded);

        if (isExpanded)
            previousExpandedPosition = position;
        holder.news_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Activity_NewsFullDetails.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        });


        holder.expand_click_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mExpandedPosition = isExpanded ? -1 : position;
                notifyItemChanged(previousExpandedPosition);
                notifyItemChanged(position);
            }
        });
        holder.share_fb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
            }
        });
        holder.share_whatsapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
            }
        });
        holder.share_twet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "WORKING!!!!", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.news_title)
        public TextView news_title;
        @BindView(R.id.news_hour)
        public TextView news_hour;
        @BindView(R.id.news_description)
        public TextView news_description;
        @BindView(R.id.news_image)
        public ImageView news_image;
        @BindView(R.id.expand_click_layout)
        RelativeLayout expand_click_layout;

        @BindView(R.id.expend_layout)
        public LinearLayout expend_layout;
        @BindView(R.id.full_title)
        public TextView full_title;
        @BindView(R.id.txt_description)
        public TextView txt_description;
        @BindView(R.id.share_twet)
        public ImageView share_twet;
        @BindView(R.id.share_whatsapp)
        public ImageView share_whatsapp;
        @BindView(R.id.share_fb)
        public ImageView share_fb;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#03000000">

<android.support.v4.widget.NestedScrollView
    android:id="@+id/nested_scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:fitsSystemWindows="false"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollbars="none">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/news_lists"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:nestedScrollingEnabled="false"              
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

ITEM XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:animateLayoutChanges="true"
android:background="@drawable/comment_background"
android:stateListAnimator="@animator/comment_selection"
android:elevation="3dp"
card_view:cardCornerRadius="2dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/news_image"
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:layout_margin="2dp"
        android:background="@drawable/icon_card"
        android:scaleType="fitXY" />

    <RelativeLayout
        android:id="@+id/expand_click_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/news_image"
        android:layout_alignTop="@+id/news_image"
        android:layout_toEndOf="@+id/news_image"
        android:layout_toRightOf="@+id/news_image">

        <TextView
            android:id="@+id/news_description"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottom_layout"
            android:layout_marginEnd="2dp"
            android:layout_marginRight="2dp"
            android:maxEms="3"
            android:maxLines="4"
            android:padding="4dp"
            android:text=""
            android:textColor="@color/black_color"
            android:textSize="18sp" />

        <RelativeLayout
            android:id="@+id/bottom_layout"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_alignParentBottom="true"
            android:layout_margin="4dp">

            <TextView
                android:id="@+id/news_hour"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@+id/center_text"
                android:layout_toStartOf="@+id/center_text"
                android:maxLines="2"
                android:text="Hours"
                android:textColor="@color/black_color"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/center_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true" />

            <TextView
                android:id="@+id/news_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toEndOf="@+id/center_text"
                android:layout_toRightOf="@+id/center_text"
                android:gravity="end"
                android:text="News Title"
                android:textColor="@color/black_color"
                android:textSize="16sp" />
        </RelativeLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/expend_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/news_image"
        android:orientation="vertical"
        android:visibility="gone">

        <TextView
            android:id="@+id/full_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Title Text"
            android:textColor="@color/black_color"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/txt_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="@string/large_text1"
            android:textColor="@color/black_color"
            android:textSize="18sp" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:orientation="horizontal">
            <ImageView
                android:id="@+id/share_fb"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/share_facebook" />
            <ImageView
                android:id="@+id/share_whatsapp"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/share_whatsapp" />

            <ImageView
                android:id="@+id/share_twet"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/share_tweet" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

尝试使用NestedScrollView代替ScrollView并在下方设置您的活动: -

recyclerView.setNestedScrollingEnabled(false);

有关详细信息,请参阅下面的 stackoverflow链接: -

How to use RecyclerView inside NestedScrollView?

Recyclerview inside ScrollView not scrolling smoothly