RecyclerView无法正确加载

时间:2017-03-16 16:41:45

标签: android android-layout android-recyclerview

我正在尝试创建一个聊天应用程序,该应用程序应该使用recyclerview和在recyclerview下面的edittext以及edittext右侧的图像按钮加载聊天。

我有一个relativelayout,我有一个recyclerview和另一个相对布局。在这第二个相对布局中,我有一个edittext和一个图像按钮。但是,Recyclerview无法正确加载。

enter image description here

我希望recyclelerview加载到edittext的开头,之间没有任何空格。

layout.xml文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_im"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.abdralabs.talksee.IMActivity"
    android:weightSum="10">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_im"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_weight="8.5"/>

    <RelativeLayout
        android:id="@+id/rl_im"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="12dp"
        android:orientation="vertical"
        android:layout_weight="1.5">

        <EditText
            android:id="@+id/et_im"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter message..."
            android:inputType="textPersonName"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:id="@+id/ib_im"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_menu_send"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>


</RelativeLayout>

activity.java文件

public class IMActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ImAdapter imAdapter;
    private List<ChatMsg> chatMsgList = new ArrayList<>();
    EditText etIp;
    Button setIp;
    Button setOn;
    EditText messageInput;
    Button sendButton;
    ServiceConnection serviceConnection;
    TalkSeeService.TalkSeeBinder talkSeeBinder;
    String otherUserName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bindService(new Intent(IMActivity.this, TalkSeeService.class), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                talkSeeBinder = (TalkSeeService.TalkSeeBinder)service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
                talkSeeBinder = null;
            }
        },BIND_AUTO_CREATE);
        setContentView(R.layout.activity_im);

        Intent intent = getIntent();
        otherUserName = intent.getStringExtra("otherUserName");
        recyclerView = (RecyclerView)findViewById(R.id.rv_im);
        imAdapter = new ImAdapter(chatMsgList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(imAdapter);

        prepareChatData();
    }


    private void prepareChatData() {
        ChatMsg chatMsg = new ChatMsg(otherUserName);
        chatMsgList.add(chatMsg);

        imAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_swipe, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch(item.getItemId())
        {
            case R.id.action_settings:
                break;
            case R.id.log_out:
                Intent i2 = new Intent(this,LauncherActivity.class);
                i2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                LoggedInSharedPreference.clearUserName(getApplicationContext());
                startActivity(i2);
                break;
            case R.id.recent_conversations:
                Intent rcIntent = new Intent(this,HistoryActivity.class);
                startActivity(rcIntent);
                break;
            default:
                Toast.makeText(this,"Somthing went wrong. Please try again!",Toast.LENGTH_SHORT).show();
                break;
        }

        return true;
    }

}

请帮帮我。感谢。

4 个答案:

答案 0 :(得分:0)

像这样编辑XML文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_im"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.abdralabs.talksee.IMActivity"
    android:weightSum="10">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_im"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_weight="8.5"/>

    <RelativeLayout
        android:id="@+id/rl_im"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="12dp"
        android:orientation="vertical"
        android:layout_weight="1.5">

        <EditText
            android:id="@+id/et_im"
            android:layout_width="290dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter message..."
            android:inputType="textPersonName"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:id="@+id/ib_im"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_menu_send"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>


</RelativeLayout>

答案 1 :(得分:0)

我认为问题是您正在更改 chatMsgList 的数据,这是本地变量。 当您调用 notifyDataSetChanged()时,它将无法工作。

要使其正常工作,请创建一个具有任何名称的新功能,并在 prepareChatData()中调用它

private void prepareChatData() {
    ChatMsg chatMsg = new ChatMsg(otherUserName);
    chatMsgList.add(chatMsg);
    imAdapter.swapData(chatMsgList);
}

在适配器内部实现此方法

public void swapData(Cursor chatMsgList) {
  myChatlistArray = chatMsgList;
  notifyDataSetChanged();
  }

答案 2 :(得分:0)

使用layout_weight属性时,您应该使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_im" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.abdralabs.talksee.IMActivity" android:weightSum="10"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_im" android:layout_width="match_parent" android:layout_height="0dp" android:scrollbars="vertical" android:layout_weight="8.5"/> <RelativeLayout android:id="@+id/rl_im" android:layout_width="match_parent" android:layout_height="0dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginBottom="12dp" android:orientation="vertical" android:layout_weight="1.5"> <EditText android:id="@+id/et_im" android:layout_width="290dp" android:layout_height="wrap_content" android:ems="10" android:hint="Enter message..." android:inputType="textPersonName" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <ImageButton android:id="@+id/ib_im" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_menu_send" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout> </LinearLayout>

C:\Programs\sonarqube-4.5.6\bin\sonar-runner.bat -Dsonar.projectBaseDir=%WORKSPACE% -Dproject.settings=D:\jenkins.space\jobs\XXX_Sonar_Runner\workspace\XXX_sonar.properties -Dsonar.branch=DEV_Jenkins -Dsonar.javascript.lcov.reportPath=XXX.Web/app/coverage/lcov.info -Dsonar.verbose=true -X && EXIT 0

答案 3 :(得分:0)

试试这个:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.abdralabs.talksee.IMActivity"
    android:id="@+id/activity_im"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_im"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/rl_im"/>

    <RelativeLayout
        android:id="@+id/rl_im"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="12dp">

        <EditText
            android:id="@+id/et_im"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter message..."
            android:inputType="textPersonName"
            android:layout_toLeftOf="@+id/ib_im"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

        <ImageButton
            android:id="@+id/ib_im"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@android:drawable/ic_menu_send"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>


</RelativeLayout>