0
我想通過使用Recyclerview和GridlayoutManager列出顏色使顏色選擇器應用程序。我使用ImageButton來呈現顏色。有兩個recyclerviews,第一個顯示我製作的主要顏色,第二個顯示第一個選定顏色的色調和陰影。我的代碼很難看,但這是用於實驗的,所以現在我只想讓gridlayoutmanager的第二個recyclerview成爲兩行四列。我可以使用下面的代碼,但它只顯示前4種顏色,而其他顏色我必須向下滾動視圖。 Android Recyclerview GridLayoutManager當行給定佈局不包裝其內容
MainActivity:
package tr.com.beyes.colorpicker;
import android.content.res.Resources;
import android.graphics.Color;
import android.support.v4.content.res.TypedArrayUtils;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources r=getResources();
int[] fontColors = r.getIntArray(R.array.fontcolors);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.fontColorView);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,11, GridLayoutManager.VERTICAL, true);
recyclerView.setLayoutManager(gridLayoutManager);
RecyclerView recyclerView2 = (RecyclerView) findViewById(R.id.fontColorViewSub);
GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this,4);
recyclerView2.setLayoutManager(gridLayoutManager2);
int[] tint = new int[8];
tint[0]=(Color.rgb(0,0,0));
int red = Color.red(fontColors[0]);
int green = Color.green(fontColors[0]);
int blue = Color.blue(fontColors[0]);
for(int i=1; i<7;i++){
red = (int) (red + (255-red) * 0.25);
green = (int) (green + (255-green) * 0.25);
blue = (int) (blue + (255-blue) * 0.25);
tint[i]=(Color.rgb(red,green,blue));
}
tint[7]=(Color.rgb(255,255,255));
ColorPickerAdapter colorPickerAdapter2 = new ColorPickerAdapter(tint,this, null);
recyclerView2.setAdapter(colorPickerAdapter2);
ColorPickerAdapter colorPickerAdapter = new ColorPickerAdapter(fontColors,this,colorPickerAdapter2);
recyclerView.setAdapter(colorPickerAdapter);
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tr.com.beyes.colorpicker.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fontColorView"
>
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fontColorViewSub"
android:layout_marginTop="20dp"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
colorbox.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="24dp"
android:id="@+id/fontColorBox"
android:clickable="true"
/>
</LinearLayout>
沒有工作。它看起來是一樣的,但唯一不同的這段時間是圖開始向下滾動到第二排。我想同時顯示2行而不滾動 – Tom
setHasFixedSize(true)to recyclerviews。檢查我更新的答案 – FAT
我也這樣做了..同樣:( – Tom