2017-08-09 67 views
-4

點擊添加按鈕後,它只顯示第一項。但是當我觸摸編輯文本字段時,剩餘的項目會顯示並消失。爲什麼Recylerview無法正確顯示數據?

這裏是在MainActivity:

package com.example.mysecondapp.myapplication; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.DefaultItemAnimator; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import java.util.ArrayList; 

public class MainActivity extends AppCompatActivity { 
    RecyclerView recyclerView; 
    EditText editText; 
    Button add,ins; 
    ArrayList<item> list; 
    Adapter adpter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     ins= (Button) findViewById(R.id.button1); 
     add= (Button) findViewById(R.id.button2); 
     editText= (EditText) findViewById(R.id.editText2); 
     recyclerView= (RecyclerView) findViewById(R.id.recycler); 
     list=new ArrayList<item>(); 
     recyclerView.setLayoutManager(new LinearLayoutManager(this)); 
     recyclerView.setItemAnimator(new DefaultItemAnimator()); 
     adpter =new Adapter(new ArrayList<item>()); 
     recyclerView.setAdapter(adpter); 


     ins.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String str=editText.getText().toString(); 
       InputMethodManager inputMethodManager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(),0); 
       editText.setText(" "); 
       Toast.makeText(getBaseContext(),"Added to list",Toast.LENGTH_SHORT).show(); 
       item i=new item(str); 
       list.add(i); 
      } 
     }); 
     add.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       adpter.adplist=list; 
       Toast.makeText(getBaseContext(),"Adding to Recycler View",Toast.LENGTH_SHORT).show(); 
       adpter.notifyDataSetChanged(); 
      } 
     }); 



     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

這裏是適配器

package com.example.mysecondapp.myapplication; 

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

import java.util.ArrayList; 

public class Adapter extends RecyclerView.Adapter<Adapter.MyHolder> { 
    ArrayList<item> adplist; 

    public ArrayList<item> getAdplist() { 
     return adplist; 
    } 

    public void setAdplist(ArrayList<item> adplist) { 
     this.adplist = adplist; 
    } 

    public Adapter(ArrayList<item>list) { 
     adplist=new ArrayList<item>(); 

     adplist=list; 
    } 

    @Override 
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.item,parent,false); 
     return new MyHolder(v); 
    } 

    @Override 
    public void onBindViewHolder(MyHolder holder, int position) { 
     item i=adplist.get(position); 
     holder.tv.setText(i.getStr()); 

    } 

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

    public class MyHolder extends RecyclerView.ViewHolder{ 
     TextView tv; 
     public MyHolder(View itemView) { 
      super(itemView); 
      tv=itemView.findViewById(R.id.item_text_view); 
     } 
    } 
} 

這裏是項目

package com.example.mysecondapp.myapplication; 

public class item { 
    String str; 

    public item(String str) { 
     this.str = str; 
    } 

    public String getStr() { 
     return str; 
    } 

    public void setStr(String str) { 
     this.str = str; 
    } 
} 

下面是活動的xml:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
     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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     tools:context="com.example.mysecondapp.myapplication.MainActivity"> 

     <android.support.design.widget.AppBarLayout 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:theme="@style/AppTheme.AppBarOverlay"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:background="?attr/colorPrimary" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.AppBarLayout> 

     <include layout="@layout/content_main"/> 

     <android.support.design.widget.FloatingActionButton 
      android:id="@+id/fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="bottom|end" 
      android:layout_margin="@dimen/fab_margin" 
      app:srcCompat="@android:drawable/ic_dialog_email" /> 

    </android.support.design.widget.CoordinatorLayout> 

下面是內容的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_main" 
    tools:context="com.example.mysecondapp.myapplication.MainActivity"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true"> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <EditText 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:ems="10" 
       android:id="@+id/editText2" 
       android:layout_weight="1" /> 

      <Button 
       android:text="INSERT" 
       android:layout_width="wrap_content" 
       android:layout_height="50dp" 
       android:id="@+id/button1" 
       android:layout_weight="2" /> 
      <Button 
       android:text="ADD" 
       android:layout_width="wrap_content" 
       android:layout_height="50dp" 
       android:id="@+id/button2" 
       android:layout_weight="2" /> 
     </LinearLayout> 

    </LinearLayout> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="60dp"/> 

</RelativeLayout> 

和項目的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/item_text_view"/> 
</LinearLayout> 

有什麼不好的代碼?

在此先感謝

+0

你想說的話,你可以請解釋一點 –

+0

回收站視圖僅僅只顯示做什麼它的數據集中的第一項。剩餘項目不顯示。 –

回答

0

試試這個: 我已經改變的LinearLayout的layout_height爲wrap_content

item.xml

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/item_text_view"/> 
    </LinearLayout> 
+0

It works.Thanks很多:) –

+0

您的歡迎! – Abhijeet

0

的問題是,你已經設置的高度回收商項目與父母匹配,這意味着回收商視圖的第一項正在全屏顯示。如果你滾動,你會發現在底部的其他項目。

爲了解決這個問題

在item.xml使用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

而不是

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent">