2017-02-27 80 views
0

Recyclerview未全屏顯示。以下是佈局文件和我寫的代碼。執行後,內容顯示如下圖所示。高度只是突出部分。我希望內容是全屏。 我剩下的內容在這個可滾動的高亮區域內。我希望內容以全屏顯示。任何幫助都會有所幫助。Recyclerview未全屏顯示

enter image description here

版式文件:

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

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

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:clickable="true"> 

     <TextView 
      android:id="@+id/title" 
      android:textSize="16dp" 
      android:textStyle="bold" 
      android:layout_alignParentTop="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/genre" 
      android:layout_below="@id/title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/year" 
      android:layout_width="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_height="wrap_content" /> 

    </RelativeLayout> 
</LinearLayout> 
  • 代碼:

    進口android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List;

    公共類setSearchData延伸RecyclerView.Adapter {

    private List<SearchDisplayContents> moviesList; 
    
    public class MyViewHolder extends RecyclerView.ViewHolder { 
        public TextView title, year, genre; 
    
        public MyViewHolder(View view) { 
         super(view); 
         title = (TextView) view.findViewById(R.id.title); 
         genre = (TextView) view.findViewById(R.id.genre); 
         year = (TextView) view.findViewById(R.id.year); 
        } 
    } 
    
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
        View itemView = LayoutInflater.from(parent.getContext()) 
          .inflate(R.layout.activity_searchresults, parent, false); 
    
        return new MyViewHolder(itemView); 
    } 
    
    @Override 
    public void onBindViewHolder(MyViewHolder holder, int position) { 
        SearchDisplayContents movie = moviesList.get(position); 
        holder.title.setText(movie.getTitle()); 
        holder.genre.setText(movie.getGenre()); 
        holder.year.setText(movie.getYear()); 
    } 
    
    @Override 
    public int getItemCount() { 
        return moviesList.size(); 
    } 
    

    }

    調用功能的代碼:

    列表movieList =新的ArrayList <>(); RecyclerView recyclerView =(RecyclerView)findViewById(R.id.recycler_view);

    setSearchData mAdapter = new setSearchData(movieList); 
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
    recyclerView.setLayoutManager(mLayoutManager); 
    recyclerView.setItemAnimator(new DefaultItemAnimator()); 
    recyclerView.setAdapter(mAdapter); 
    
    SearchDisplayContents movie = new SearchDisplayContents("Mad Max: Fury Road", "Action & Adventure", "2015"); 
    movieList.add(movie); 
    
    movie = new SearchDisplayContents("Inside Out", "Animation, Kids & Family", "2015"); 
    movieList.add(movie); 
    
    mAdapter.notifyDataSetChanged(); 
    
+0

請出示整個佈局文件 – xklakoux

+0

佈局文件已完成。請檢查 – Nikhil

+0

請告訴我們:activity_searchresults.xml – xklakoux

回答

1

你應該爲你的viewholder使用不同的佈局。把你有的RelativeLayout剪下來,放在不同的xml文件中,然後傳給onCreateViewHolder。

+0

我也試過,如果它是match_parent,屏幕獲得全視圖但沒有內容顯示 – Nikhil

0

您應該爲您的視圖使用不同的佈局。把你有的RelativeLayout剪下來,放在不同的xml文件中,然後傳給onCreateViewHolder。

+0

對不起,我編輯了您的答案,並且無法撤消它。雖然有人接受,但不知道爲什麼 – xklakoux

0

看看你的代碼 - 它看起來像預覽屏幕截圖中的內容與你已經靜態定義的RelativeLayout中的內容相同。

您的方法存在問題。它是在RecyclerView.Adapter中引用RelativeLayout(在RecylerView的同一個xml層次結構中)作爲CellViewHolder。我個人的方法是爲單元格視圖創建一個單獨的xml佈局。

按照本指南,這是非常詳細: https://guides.codepath.com/android/using-the-recyclerview

0

上述編碼已經什麼工作使用不同的佈局意味着一個Recycleview和其他在名單

main_layout.xml

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

    <android.support.v7.widget.RecyclerView 
    android:id="@+id/uslist" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="@dimen/_49sdp" 
    android:divider="@android:color/transparent" /> 
</RelativeLayout> 

list_content。XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="true" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="10dp" 
    android:paddingBottom="10dp" 
    android:clickable="true"> 

    <TextView 
     android:id="@+id/title" 
     android:textSize="16dp" 
     android:textStyle="bold" 
     android:layout_alignParentTop="true" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/genre" 
     android:layout_below="@id/title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/year" 
     android:layout_width="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_height="wrap_content" /> 

</RelativeLayout> 

Adapter.java

public class yourAdapter extends RecyclerView.Adapter<yourAdapter .SimpleViewHolder> { 


ArrayList<UsageRPDetails> mylist; 
private Context mContext; 

public yourAdapter (Context context, ArrayList<yourArray or model> checklist) { 
    mContext = context; 
    mylist = checklist; 
} 

@Override 
public yourAdapter .SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_content, parent, false); 
    return new yourAdapter .SimpleViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final yourAdapter .SimpleViewHolder holder, final int position) { 
    holder.title.setText(); 
    holder.year.setText(); 
    holder.genre.setText(); 


} 

@Override 
public long getItemId(int i) { 
    return 0; 
} 

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

@Override 
public int getItemViewType(int i) { 
    return 0; 
} 

public static class SimpleViewHolder extends RecyclerView.ViewHolder { 
    @Bind(R.id.title) 
    TextView title; 
    @Bind(R.id.genre) 
    TextView genre; 
@Bind(R.id.year) 
    TextView year; 


    public SimpleViewHolder(View itemView) { 
     super(itemView); 
     title= (TextView) itemView.findViewById(R.id.title); 
     genre= (TextView) itemView.findViewById(R.id.genre); 
     genre= (TextView) itemView.findViewById(R.id.genre); 


    } 
} 

}

還寫活動的Java文件調用此適配器類,並調用recycleview這項活動