2017-10-04 3 views
0

항목 목록을 만들고 싶지만 임의로 배치 된 것처럼 다른 방식으로 정렬하고 싶습니다. 상대적으로 API의 항목 수가 동적으로 변경됩니다. 것은 내가 정확히 다음과 같이 무작위로 배치 방식으로 들어오는 모든 항목을 배치 할 수 있습니다 방법이다 : ImageSample임의로 배치 한 항목이있는 Android RecycleView

enter image description here

P.S. 3 가지 유형의 그림이 있습니다 : 수평 직사각형, 수직 직사각형 및 정사각형. 당신이 hoz 예에 따라, 그것을 사용하는 모르는 경우

답변

0

여러 뷰 홀더 패턴을 확인하고 특정 변수에 기초 어댑터 내부의 뷰를 팽창. 3 가지 유형의 행이 있으므로 3 개의 뷰 소유자를 생성해야합니다

+0

시도해 보았지만 열의 난수로 요소를 배치하는 방법을 모릅니다. – revcik

+0

임의의 열 수를 의미합니까? – YoLo

0
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.StaggeredGridLayoutManager; 
import java.util.ArrayList; 
import java.util.Arrays; 
public class MainActivity extends AppCompatActivity { 
    // ArrayList for person names 
    ArrayList personNames = new ArrayList<>(Arrays.asList("Person 1", "Person 2", "Person 3", "Person 4", "Person 5", "Person 6", "Person 7", "Person 8", "Person 9", "Person 10", "Person 11", "Person 12", "Person 13", "Person 14")); 
    ArrayList personImages = new ArrayList<>(Arrays.asList(R.drawable.person1, R.drawable.person2, R.drawable.person3, R.drawable.person4, R.drawable.person5, R.drawable.person6, R.drawable.person7, R.drawable.person1, R.drawable.person2, R.drawable.person3, R.drawable.person4, R.drawable.person5, R.drawable.person6, R.drawable.person7)); 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // get the reference of RecyclerView 
     RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     // set a StaggeredGridLayoutManager with 3 number of columns and vertical orientation 
     StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL); 
     recyclerView.setLayoutManager(staggeredGridLayoutManager); // set LayoutManager to RecyclerView 
     // call the constructor of CustomAdapter to send the reference and data to Adapter 
     CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, personNames, personImages); 
     recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView 
    } 
} 

CustomAdapter.java

import android.content.Context; 
import android.content.Intent; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.TextView; 
import java.util.ArrayList; 
public class CustomAdapter extends RecyclerView.Adapter { 
    ArrayList personNames; 
    ArrayList personImages; 
    Context context; 
    public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) { 
     this.context = context; 
     this.personNames = personNames; 
     this.personImages = personImages; 
    } 
    @Override 
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     // infalte the item Layout 
     View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false); 
     // set the view's size, margins, paddings and layout parameters 
     MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder 
     return vh; 
    } 
    @Override 
    public void onBindViewHolder(MyViewHolder holder, final int position) { 
     // set the data in items 
     holder.name.setText(personNames.get(position)); 
     holder.image.setImageResource(personImages.get(position)); 
     // implement setOnClickListener event on item view. 
     holder.itemView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // open another activity on item click 
       Intent intent = new Intent(context, SecondActivity.class); 
       intent.putExtra("image", personImages.get(position)); // put image data in Intent 
       context.startActivity(intent); // start Intent 
      } 
     }); 
    } 
    @Override 
    public int getItemCount() { 
     return personNames.size(); 
    } 
    public class MyViewHolder extends RecyclerView.ViewHolder { 
     // init the item view's 
     TextView name; 
     ImageView image; 
     public MyViewHolder(View itemView) { 
      super(itemView); 
      // get the reference of item view's 
      name = (TextView) itemView.findViewById(R.id.name); 
      image = (ImageView) itemView.findViewById(R.id.image); 
     } 
    } 
} 

열기 고해상도를 - > activity_main.xml (또는) main.xml에 다음 코드를 추가하십시오.

우리는 XML 파일에서 RecyclerView를 생성합니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</RelativeLayout> 

RecyclerView의 그리드 항목에 대해 새 XML 파일 rowlayout.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:background="@drawable/custom_item_layout" 
    android:padding="5dp"> 
    <!-- 
    grid items for RecyclerView 
    --> 
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:src="@mipmap/ic_launcher" /> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="ABCD" 
     android:textSize="20sp" 
     android:textColor="#fff" /> 
</RelativeLayout> 

enter image description here

+0

은 좋아 보이지만 여기에는 3 열도 있습니다. 이 경우 난 임의의 수의 열에 배치해야합니다. – revcik

+0

이 레이아웃은 고정 된 스팬이있는 격자입니다. 그러나 전체 행 또는 열에서 항목을 확장 할 수 있습니다. 어떻게 작동하는지 봅시다. 이전의 활성/비활성 항목을 사용하여 활성 항목을 완전히 확장합니다. 이 작업은 항목을 바인딩 할 때 어댑터에서 수행됩니다. –

+0

// 활성 상태 인 경우 항목에 스팬합니다. final ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams(); if (lp instanceof StaggeredGridLayoutManager.LayoutParams) { StaggeredGridLayoutManager.LayoutParams sglp = (StaggeredGridLayoutManager.LayoutParams) lp; sglp.setFullSpan (item.isActive()); holder.itemView.setLayoutParams (sglp); } –