我真的需要帮助,我想要一个像Image here这样的界面。我用LinearLayout完成了它,但我想用GridView做,所以请帮助我。我从数据库中取出图片。
这是我的活动java:
public class GestionImage {
public static void loadImageFromUr(Context context, String url, ImageView image) {
Glide.with(context)
.load(url)
.asBitmap()
.into(new BitmapImageViewTarget(image) {
@Override
protected void setResource(Bitmap resource) {
super.setResource(resource);
}
});
}
}
MainActivity.java 代码:
public class MainActivity extends AppCompatActivity {
ArrayList<CategorieEntity> listCategories;
LinearLayout layoutBtn;
ArrayList<LinearLayout> listlayoutBtnText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listCategories = new ArrayList<CategorieEntity>();
CategorieEntity fruit = new CategorieEntity(1, "Fruit", "desc cate 1", "https://openclipart.org/image/2400px/svg_to_png/280304/set-icone-frutta.png");
CategorieEntity legume = new CategorieEntity(2, "Legumes", "desc 2", "https://icon-icons.com/icons2/53/PNG/256/fruits_vegetables_10762.png");
listCategories.add(fruit);
listCategories.add(legume);
layoutBtn = (LinearLayout) findViewById(R.id.layoutImageButton);
listlayoutBtnText = new ArrayList<LinearLayout>();
for (int i = 0; i < listCategories.size(); i++) {
//imageCategories.setScaleType(ImageView.ScaleType.FIT_XY);
ImageView imageCategories = new ImageView(getApplicationContext());
GestionImage.loadImageFromUr(getApplicationContext(), listCategories.get(i).getImageCategorie(), imageCategories);
TextView libelleCategories = new TextView(getApplicationContext());
libelleCategories.setText(listCategories.get(i).getLibelleCategorie());
LinearLayout layoutBtnText = new LinearLayout(getApplicationContext());
layoutBtnText.addView(imageCategories);
layoutBtnText.addView(libelleCategories);
listlayoutBtnText.add(layoutBtnText);
final int finalI = i;
imageCategories.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
long idCategorie = listCategories.get(finalI).getIdCategorie();
Intent intent = new Intent(getApplicationContext(), ListSelonCategorie.class);
intent.putExtra("idCategories", idCategorie);
startActivity(intent);
}
});
}
for (int i = 0; i < listlayoutBtnText.size(); i++) {
layoutBtn.addView(listlayoutBtnText.get(i));
}
}
}
答案 0 :(得分:0)
从空活动开始。您将执行以下任务以添加RecyclerView。您需要做的就是复制并粘贴每个部分中的代码。稍后您可以根据自己的需要进行自定义。
在您的活动中初始化RecyclerView
确保您的app gradle.build文件中包含以下依赖项:
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
将RecyclerView
添加到xml布局。
activity_main.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.support.v7.widget.RecyclerView
android:id="@+id/rvNumbers"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
RecyclerView
网格中的每个单元格只有一个TextView
。创建一个新的布局资源文件。
recyclerview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:padding="5dp"
android:layout_width="50dp"
android:layout_height="50dp">
<TextView
android:id="@+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent"/>
</LinearLayout>
RecyclerView
需要adapter
使用您的数据填充每个单元格中的视图。创建一个新的java文件。
MyRecyclerViewAdapter.java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
private String[] mData = new String[0];
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
// data is passed into the constructor
MyRecyclerViewAdapter(Context context, String[] data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
}
// inflates the cell layout from xml when needed
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_item, parent, false);
return new ViewHolder(view);
}
// binds the data to the textview in each cell
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String animal = mData[position];
holder.myTextView.setText(animal);
}
// total number of cells
@Override
public int getItemCount() {
return mData.length;
}
// stores and recycles views as they are scrolled off screen
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ViewHolder(View itemView) {
super(itemView);
myTextView = (TextView) itemView.findViewById(R.id.info_text);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
// convenience method for getting data at click position
String getItem(int id) {
return mData[id];
}
// allows clicks events to be caught
void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
虽然不是绝对必要,但我还提供了监听单元格上的点击事件的功能。这在旧的GridView中可用,并且是常见的需求。如果您不需要,可以删除此代码。
将以下代码添加到main activity
。
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ItemClickListener {
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// data to populate the RecyclerView with
String[] data = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48"};
// set up the RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rvNumbers);
int numberOfColumns = 6;
recyclerView.setLayoutManager(new GridLayoutManager(this, numberOfColumns));
adapter = new MyRecyclerViewAdapter(this, data);
adapter.setClickListener(this);
recyclerView.setAdapter(adapter);
}
@Override
public void onItemClick(View view, int position) {
Log.i("TAG", "You clicked number " + adapter.getItem(position) + ", which is at cell position " + position);
}
}
请注意,该活动实现了我们在适配器中定义的ItemClickListener
。这允许我们处理onItemClick
中的单元格点击事件。
答案 1 :(得分:0)
感谢您的回复,但如何更换此字符串[]数据= {&#34; 1&#34;,&#34; 2&#34;,&#34; 3&#34;,&#34; 4&# 34;,&#34; 5&#34;,&#34; 6&#34;,&#34; 7&#34;,&#34; 8&#34;,&#34; 9&#34;,&# 34; 10&#34;,&#34; 11&#34;,&#34; 12&#34;,&#34; 13&#34;,&#34; 14&#34;,&#34; 15&#34 ;,&#34; 16&#34;,&#34; 17&#34;,&#34; 18&#34;,&#34; 19&#34;,&#34; 20&#34;,&#34 ; 21&#34;,&#34; 22&#34;,&#34; 23&#34;,&#34; 24&#34;,&#34; 25&#34;,&#34; 26&#34; ,&#34; 27&#34;,&#34; 28&#34;,&#34; 29&#34;,&#34; 30&#34;,&#34; 31&#34;,&#34; 32&#34;,&#34; 33&#34;,&#34; 34&#34;,&#34; 35&#34;,&#34; 36&#34;,&#34; 37&#34;, &#34; 38&#34;,&#34; 39&#34;,&#34; 40&#34;,&#34; 41&#34;,&#34; 42&#34;,&#34; 43& #34;,&#34; 44&#34;,&#34; 45&#34;,&#34; 46&#34;,&#34; 47&#34;,&#34; 48&#34;}; dynamique。我有类别表(Id_categorry,名称,图像)
所以我需要显示类别的图片,当我添加一个新类别时,它会自动显示