1

CardView가 포함 된 RecyclerView을 만들고 싶습니다. 이를 위해 ThreadonCreate이 내 Activity.에 있습니다. 내 서버에서 데이터를 가져 와서 목록에 넣으십시오. 그런 다음 RecyclerView,에 대한 어댑터를 만들지 만 작동하지 않습니다. 여기안드로이드 | RecyclerView가 작동하지 않습니다

내가 어댑터를 생성하고 모든 CardView의를 작성해야합니다 : 여기 내 코드입니다

: 여기
ListAdapter adapter = new ListAdapter(posts); 
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list); 
rv.setAdapter(adapter); 

내 어댑터 클래스 다음

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.PostViewHolder> { 

List<Post> posts; 

public ListAdapter(List<Post> posts) { 
    Log.d("ListAdapter", ""); 
    this.posts = posts; 
} 

@Override 
public int getItemCount() { 
    Log.d("getItemCount", ""); 
    return posts.size(); 
} 

@Override 
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
    Log.d("onCreateView", ""); 
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.insert_layout, viewGroup, false); 
    PostViewHolder pvh = new PostViewHolder(v); 
    return pvh; 
} 

@Override 
public void onBindViewHolder(PostViewHolder postViewHolder, int i) { 
    Log.d("onBindView", ""); 
    postViewHolder.username.setText(posts.get(i).getUsername()); 
    postViewHolder.text.setText(posts.get(i).getText()); 
    postViewHolder.time.setText(Long.toString(posts.get(i).getTime())); 
    postViewHolder.postPhoto.setImageResource(posts.get(i).returnIMG()); 
} 

@Override 
public void onAttachedToRecyclerView(RecyclerView recyclerView) { 
    Log.d("onAttached", ""); 
    super.onAttachedToRecyclerView(recyclerView); 
} 


public static class PostViewHolder extends RecyclerView.ViewHolder { 
    CardView cv; 
    TextView username; 
    TextView time; 
    TextView text; 
    ImageView postPhoto; 

    PostViewHolder(View itemView) { 
     super(itemView); 
     Log.d("PostViewHolder", ""); 
     cv = (CardView) itemView.findViewById(R.id.cv); 
     username = (TextView) itemView.findViewById(R.id.usernameText); 
     time = (TextView) itemView.findViewById(R.id.timeText); 
     text = (TextView) itemView.findViewById(R.id.textText); 
     postPhoto = (ImageView) itemView.findViewById(R.id.postPhoto); 
    } 
} 

는 것은 내 recyclerview입니다

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


</android.support.v7.widget.RecyclerView> 

내 카드보기 :

여기

그리고

<android.support.v7.widget.CardView android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/cv" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      > 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/postPhoto" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/textText" 
       android:layout_alignParentTop="true" 
       android:textSize="30sp" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/usernameText"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:id="@+id/timeText"/> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 
2 오류 :

E/RecyclerView : 첨부 된 어댑터; 레이아웃 건너 뛰기 E/RecyclerView : 레이아웃 관리자가 연결되지 않았습니다. 레이아웃을 건너 뛰는 중

하지만 나는 둘 다하지 않습니까?

+0

을 당신이 레이아웃 매니저를 설정하는 경우? –

+0

레이아웃 관리자를 설정하지 않았습니다. – EpicPandaForce

답변

1

을 먼저 LayoutManager으로 설정하십시오. 코드에서

:

ListAdapter adapter = new ListAdapter(posts); 
RecyclerView rv = (RecyclerView)findViewById(R.id.post_list); 
rv.setHasFixedSize(true); 

LinearLayoutManager layoutManager = new LinearLayoutManager(this); 
layoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
rv.setLayoutManager(layoutManager); 

rv.setAdapter(adapter); 

희망이 당신을 도울 것입니다! ..

관련 문제