Android recycerview不显示arraylist数据
您好,我正在尝试使用MVVM制作一个片段,并从我的API调用,列出故事和userpost/picture,但目前故事运行良好,我可以看到它也从API获取数据,并将数据发送到userpost/pictures,但我没有将其插入视图,希望这里有人能帮助我:) 我的片段中的代码:Android recycerview不显示arraylist数据,android,api,android-recyclerview,Android,Api,Android Recyclerview,您好,我正在尝试使用MVVM制作一个片段,并从我的API调用,列出故事和userpost/picture,但目前故事运行良好,我可以看到它也从API获取数据,并将数据发送到userpost/pictures,但我没有将其插入视图,希望这里有人能帮助我:) 我的片段中的代码: private ArrayList<StoryList> storyArrayList; private ArrayList<FeedData> feedArrayList; private Feed
private ArrayList<StoryList> storyArrayList;
private ArrayList<FeedData> feedArrayList;
private FeedViewModel feedViewModel;
private StoryAdapter storyAdapter;
private FeedAdapter feedAdapter;
private LinearLayoutManager newsFeedLayoutManager;
private void setupView() {
mBinding.storyRecyler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
storyArrayList = new ArrayList<>();
storyAdapter = new StoryAdapter(getActivity(), storyArrayList);
mBinding.storyRecyler.setAdapter(storyAdapter);
newsFeedLayoutManager = new LinearLayoutManager(getContext());
mBinding.recylerFeed.setLayoutManager(newsFeedLayoutManager);
feedArrayList = new ArrayList<>();
feedAdapter = new FeedAdapter(getActivity(), feedArrayList);
mBinding.recylerFeed.setAdapter(feedAdapter);
mBinding.recylerFeed.hasFixedSize();
mBinding.recylerFeed.setItemViewCacheSize(10);
}
@Override
public void storyresponse(Object object) {
if (object instanceof StoryResponse) {
StoryResponse response = (StoryResponse) object;
storyArrayList.addAll(response.getData());
storyAdapter.notifyDataSetChanged();
}
if (object instanceof FeedResponse) {
FeedResponse feedresponse = (FeedResponse) object;
feedArrayList.addAll(feedresponse.getFeeddata());
feedAdapter.notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res/android">
<data>
</data>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_marginTop="0dp"
android:background="#ffff"
android:layout_marginBottom="-2dp"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
app:layout_collapseMode="parallax"
android:layout_marginTop="5dp"
android:layout_height="match_parent">
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#efefef"/>
<LinearLayout
android:id="@+id/storiesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:paddingBottom="6dp"
android:background="#ffff"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/storyRecyler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="0dp"
android:scrollbars="vertical"
android:layout_marginEnd="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="@+id/material_style_ptr_frame"
android:layout_marginBottom="48dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerFeed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-4dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="48dp"
android:scrollbars="vertical"
android:background="#f3f3f3"
android:layout_marginEnd="-5dp" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<ProgressBar
android:layout_centerInParent="true"
android:id="@+id/sectionProgress"
android:layout_width="50dp"
android:layout_height="50dp" >
</ProgressBar>
<com.hitomi.cmlibrary.CircleMenu
android:id="@+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</layout>
私有ArrayList storyArrayList;
私有ArrayList feedArrayList;
私有FeedViewModel FeedViewModel;
私有故事适配器;
专用馈线适配器;
私人专线布局经理newsFeedLayoutManager;
私有void setupView(){
mBinding.storyRecyler.setLayoutManager(新的LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
storyArrayList=新建ArrayList();
storyAdapter=新的storyAdapter(getActivity(),storyArrayList);
mBinding.storyRecyler.setAdapter(storyAdapter);
newsFeedLayoutManager=newlinearlayoutmanager(getContext());
mBinding.recylerFeed.setLayoutManager(newsFeedLayoutManager);
feedArrayList=新的ArrayList();
feedAdapter=新的feedAdapter(getActivity(),feedArrayList);
mBinding.recylerFeed.setAdapter(feedAdapter);
mBinding.recylerFeed.hasFixedSize();
mBinding.recylerFeed.setItemViewCacheSize(10);
}
@凌驾
公共void故事响应(对象){
if(StoryResponse的对象实例){
StoryResponse=(StoryResponse)对象;
storyArrayList.addAll(response.getData());
storyAdapter.notifyDataSetChanged();
}
if(FeedResponse的对象实例){
FeedResponse FeedResponse=(FeedResponse)对象;
addAll(feedresponse.getFeeddata());
feedAdapter.notifyDataSetChanged();
}
}
和适配器代码:
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.ViewHolder> {
private final List<FeedData> feedList;
private Activity context;
private View.OnClickListener onClickListener;
public FeedAdapter(Activity context, List<FeedData> feedList) {
this.context = context;
this.feedList = feedList;
this.onClickListener = onClickListener;
}
public List<FeedData> getFeedData(){
return feedList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_feed_photo, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.mBinding.setFeed(feedList.get(position));
Boolean isLiked = holder.mBinding.getFeed().getIsLiked();
String username = holder.mBinding.getFeed().getUsername();
holder.mBinding.username.setText(username);
}
@Override
public int getItemCount() {
return feedList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ListItemFeedPhotoBinding mBinding;
public ViewHolder(View itemView) {
super(itemView);
mBinding = DataBindingUtil.bind(itemView);
}
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private FeedAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final FeedAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
}
公共类FeedAdapter扩展了RecyclerView.Adapter{
私人最终名单;
私人活动语境;
private View.OnClickListener OnClickListener;
公共FeedAdapter(活动上下文,列表feedList){
this.context=上下文;
this.feedList=feedList;
this.onClickListener=onClickListener;
}
公共列表getFeedData(){
返回反馈列表;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
View View=LayoutInflater.from(parent.getContext()).flate(R.layout.list\u item\u feed\u photo,parent,false);
返回新的ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
holder.mBinding.setFeed(feedList.get(position));
Boolean isLiked=holder.mBinding.getFeed().getIsLiked();
字符串username=holder.mBinding.getFeed().getUsername();
holder.mBinding.username.setText(用户名);
}
@凌驾
public int getItemCount(){
返回feedList.size();
}
类ViewHolder扩展了RecyclerView.ViewHolder{
ListItemFeedPhotoBinding;
公共视图持有者(视图项视图){
超级(项目视图);
mBinding=DataBindingUtil.bind(itemView);
}
}
公共界面ClickListener{
void onClick(视图,int位置);
仅长按无效(视图,int位置);
}
公共静态类RecyclerTouchListener实现RecyclerView.OnItemTouchListener{
私人手势检测器;
私有FeedAdapter.ClickListener ClickListener;
公共RecyclerTouchListener(上下文上下文、最终RecyclerView RecyclerView、最终FeedAdapter.ClickListener ClickListener){
this.clickListener=clickListener;
gestureDetector=new gestureDetector(上下文,new gestureDetector.SimpleOnGestureListener()){
@凌驾
公共布尔onSingleTapUp(运动事件e){
返回true;
}
@凌驾
公开无效在线新闻(运动事件e){
View child=recyclerView.findChildViewUnder(e.getX(),e.getY());
if(child!=null&&clickListener!=null){
clickListener.onLongClick(child,recyclerView.getChildPosition(child));
}
}
});
}
@凌驾
公共布尔值onInterceptTouchEvent(RecyclerView rv,MotionEvent e){
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null&&clickListener!=null&&gestureDetector.onTouchEvent(e)){
单击listener.onClick(child,rv.getChildPosition(child));
}
返回false;
}
@凌驾
公共活动(RecyclerView rv、MotionE){
}
@凌驾
OnRequestDisallowinetCeptTouchEvent上的公共无效(布尔值DisallowinetCept){
}
}
}
片段的XML:
private ArrayList<StoryList> storyArrayList;
private ArrayList<FeedData> feedArrayList;
private FeedViewModel feedViewModel;
private StoryAdapter storyAdapter;
private FeedAdapter feedAdapter;
private LinearLayoutManager newsFeedLayoutManager;
private void setupView() {
mBinding.storyRecyler.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
storyArrayList = new ArrayList<>();
storyAdapter = new StoryAdapter(getActivity(), storyArrayList);
mBinding.storyRecyler.setAdapter(storyAdapter);
newsFeedLayoutManager = new LinearLayoutManager(getContext());
mBinding.recylerFeed.setLayoutManager(newsFeedLayoutManager);
feedArrayList = new ArrayList<>();
feedAdapter = new FeedAdapter(getActivity(), feedArrayList);
mBinding.recylerFeed.setAdapter(feedAdapter);
mBinding.recylerFeed.hasFixedSize();
mBinding.recylerFeed.setItemViewCacheSize(10);
}
@Override
public void storyresponse(Object object) {
if (object instanceof StoryResponse) {
StoryResponse response = (StoryResponse) object;
storyArrayList.addAll(response.getData());
storyAdapter.notifyDataSetChanged();
}
if (object instanceof FeedResponse) {
FeedResponse feedresponse = (FeedResponse) object;
feedArrayList.addAll(feedresponse.getFeeddata());
feedAdapter.notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:bind="http://schemas.android.com/apk/res/android">
<data>
</data>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffff"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_marginTop="0dp"
android:background="#ffff"
android:layout_marginBottom="-2dp"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
app:layout_collapseMode="parallax"
android:layout_marginTop="5dp"
android:layout_height="match_parent">
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#efefef"/>
<LinearLayout
android:id="@+id/storiesLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginTop="5dp"
android:paddingBottom="6dp"
android:background="#ffff"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/storyRecyler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="0dp"
android:scrollbars="vertical"
android:layout_marginEnd="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="@+id/material_style_ptr_frame"
android:layout_marginBottom="48dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerFeed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="-4dp"
android:layout_alignParentTop="true"
android:layout_marginBottom="48dp"
android:scrollbars="vertical"
android:background="#f3f3f3"
android:layout_marginEnd="-5dp" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<ProgressBar
android:layout_centerInParent="true"
android:id="@+id/sectionProgress"
android:layout_width="50dp"
android:layout_height="50dp" >
</ProgressBar>
<com.hitomi.cmlibrary.CircleMenu
android:id="@+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone" />
</RelativeLayout>
</layout>
我看不到应该向适配器添加项的代码。找不到数据插入代码?????抱歉,忘记了将添加项添加到适配器,但它现在就在那里:)您调试了storyresponse
方法了吗?运行时对象的类型是什么?可以发布布局xml文件吗?