2017-10-04 95 views
0

想要製作物品列表,但按不同方式排序,如隨機放置。項目數量將會動態變化,相對API。事情是,我怎麼能放在隨意擺放方式所有進入的項目,恰恰是這樣的:ImageSampleAndroid RecycleView隨機放置物品

enter image description here

附:將會有3種類型的圖形:水平矩形,垂直矩形和方形。

回答

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(); (StaggeredGridLayoutManager.LayoutParams的實例) sglp.setFullSpan(item.isActive()); holder.itemView.setLayoutParams(sglp); } –