Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 显示单个项目的回收器视图_Android_Android Recyclerview - Fatal编程技术网

Android 显示单个项目的回收器视图

Android 显示单个项目的回收器视图,android,android-recyclerview,Android,Android Recyclerview,我面临一个奇怪的错误,recyclerview只显示一个项目。下面是我的recyclerview适配器的代码: public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { List<chat> chats; String username; final int My=0,Their=1; public ChatAdapter(List<chat> cha

我面临一个奇怪的错误,recyclerview只显示一个项目。下面是我的recyclerview适配器的代码:

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


List<chat> chats;
String username;
final int My=0,Their=1;

public ChatAdapter(List<chat> chats) {
    this.chats = chats;
    this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder viewHolder;
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    switch (viewType) {
        case My:
            View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v1);
            break;
        case Their:
            View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
            viewHolder = new TheirMessageHolder(v2);
            break;
        default:
            View v = inflater.inflate(R.layout.my_chat_child, parent, false);
            viewHolder = new MyChatHolder(v);
            break;
    }

    return viewHolder;

}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    switch (holder.getItemViewType()) {
        case My:
            MyChatHolder myChatHolder = (MyChatHolder) holder;
            myChatHolder.setMyMessage(chats.get(position).getMessage());
            break;

        case Their:
            TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
            theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
            break;
    }
}

@Override
public int getItemViewType(int position) {
    if(username.equalsIgnoreCase(chats.get(position).getFrom()))
        return My;
    return Their;
}

@Override
public int getItemCount() {
    return chats.size();
}}
公共类ChatAdapter扩展了RecyclerView.Adapter{
列表聊天;
字符串用户名;
最终int My=0,其=1;
公共聊天适配器(列表聊天){
this.chats=聊天;
this.username=PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
RecyclerView.ViewHolder-ViewHolder;
LayoutInflater充气器=LayoutInflater.from(parent.getContext());
开关(视图类型){
我的情况:
视图v1=充气机。充气(R.layout.my_chat_child,parent,false);
viewHolder=新的MyChatHolder(v1);
打破
他们的情况:
视图v2=充气机。充气(R.layout.this_chat_child,parent,false);
viewHolder=新的消息保持器(v2);
打破
违约:
视图v=充气机。充气(R.layout.my_chat_child,parent,false);
viewHolder=新的MyChatHolder(v);
打破
}
返回视图持有者;
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
开关(holder.getItemViewType()){
我的情况:
MyChatHolder MyChatHolder=(MyChatHolder)持有者;
setMyMessage(chats.get(position.getMessage());
打破
他们的情况:
TheirMessageHolder TheirMessageHolder=(TheirMessageHolder)holder;
theirMessageHolder.setTheirChat(chats.get(position.getFrom(),chats.get(position.getMessage());
打破
}
}
@凌驾
public int getItemViewType(int位置){
if(username.equalsIgnoreCase(chats.get(position.getFrom()))
归还我的;
归还他们的财产;
}
@凌驾
public int getItemCount(){
返回chats.size();
}}
我已经在其他应用程序中使用了这段代码,它工作得非常好。我已经检查了聊天数据,这也是完美的

以下是指向git repo布局文件的链接:

不要在项目视图中使用
match\u parent
作为高度。一个项目垂直填充整个屏幕,所以您看不到另一个项目

为recyclerview创建row.xml时,应遵循以下步骤:

  • 始终使用“换行内容”作为行的高度,否则在“匹配父项”中,它将占据整行的整个屏幕

  • 您也可以采用dp表示的高度

  • 1) 如果您的recyclerview是垂直的,则设置recyclerview的高度
    match\u parent
    row\u item.xml
    高度也
    match\u parent

    2) 如果您的recyclerview是水平的,则设置recyclerview的宽度
    match\u parent
    row\u item.xml
    宽度也
    match\u parent

    例如:- 水平循环视图

    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="60dp" />
    
    
    
    row_item.xml

    <TextView
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/rect"
    android:gravity="center"
    android:maxLines="2"
    android:padding="10dp"
    android:textColor="@color/white"
    android:textSize="25sp" />
    

    尝试将项目视图中使用的布局更改为
    FrameLayout
    。这里有一个例子

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/item"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeight"
        android:clickable="true"
        android:focusable="true"
        android:foreground="?selectableItemBackground">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"/>
    </FrameLayout>
    

    content\u main.xml如下

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    
    在row_list.xml文件中进行以下更改

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    

    我在运行时进行了上述更改。

    我的错误是我不小心使用了:

    LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
    
    而不是:

    LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
    

    检查日志后,确保在列表中添加了多个项目,但在UI中仅显示1个项目意味着检查此属性。 在item_xml文件中将属性更改为

    正确的方法

    错误的方法


    @o如果我添加了git repo链接,请尝试在布局@Slavik中设置高度和宽度的
    wrap\u content
    而不是
    match\u parent
    ,谢谢!谷歌请添加一些皮棉警告,让你讨厌的皮棉有用这一次!哦,谢谢,真棒的人,在我的例子中,我创建了一个以match_父母为高度的项目。因此,所有屏幕都被保留在第一项后面。让我给你买杯咖啡吧!你救了我的命!谢谢不适合我。仅当
    按钮
    置于
    回收视图
    之前时,才会显示所有项目。有什么建议吗?我也有同样的问题,我试着滚动,看到了其余的项目。这也是我的错误。谢谢你。
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
    
     android:layout_width="match_parent"
     android:layout_height="match_parent"