如何正确构建GridViewAdapter?

时间:2017-03-15 12:51:45

标签: android gridview android-adapter gridlayoutmanager main-activity

我尝试创建一个带有6个按钮的GridView,我想在按钮上显示6个不同的名称。

这是我的带有GridView的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.aaron.waller.girdlayoutmanagertest.MainActivity">


    <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gridView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:numColumns="auto_fit"
        android:horizontalSpacing="10dp"
        android:verticalSpacing="10dp"
        android:columnWidth="120dp"
        android:stretchMode="spacingWidthUniform"/>
</RelativeLayout>

这是single_item.xml(在这种情况下,它只是一个普通的Button):

<?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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />
</RelativeLayout>

这是我的strings.xml,有6个名字:

<resources>
    <string name="app_name">GirdLayoutManagerTest</string>
    <string-array name="button_names">
        <item>Name1</item>
        <item>Name2</item>
        <item>Name3</item>
        <item>Name4</item>
        <item>Name5</item>
        <item>Name6</item>
    </string-array>
</resources>

最后是我的MainActivity.java:

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridView =(GridView)findViewById(R.id.gridView);
        gridView.setAdapter(new GridAdapter(this));
    }
}
class ButtonNames{

    String buttonNames;
    ButtonNames(String buttonNames){

        this.buttonNames = buttonNames;
    }
}
class GridAdapter extends BaseAdapter{

    ArrayList<ButtonNames> list;
    Context context;
    GridAdapter(Context context){
        this.context = context;
        list= new ArrayList<ButtonNames>();
        Resources res = context.getResources();
        String[] tempButtonNames = res.getStringArray(R.array.button_names);
        for(int i = 0; i<6; i++){
            ButtonNames tempButton = new ButtonNames(tempButtonNames[i]);
            list.add(tempButton);
        }

    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View row = view;
        if(row == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.single_item, viewGroup, false);


        }else{
            row.getTag();
        }

        return row;
    }
}

我刚收到这个输出: https://gyazo.com/1495d3f0c11e6eb0e903e79a68e7f2b0

如何在按钮上设置6个名称?

P.S我是Android Studio新手。

2 个答案:

答案 0 :(得分:0)

更新GridAdapter的getView()方法:

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View row = view;
    if(row == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.single_item, viewGroup, false);

        //ADD THESE LINES
        Button btn = (Button) row.findViewById(R.id.button);
        btn.setText(list.get(i));

    }else{
        row.getTag();
    }

    return row;
}

答案 1 :(得分:0)

为什么要使用Gridview.Android为Recyclerview提供此功能。如果您接触到recyclerview,您只需要设置GridLayoutManager而不是LinearLayoutManager。

GridLayoutManager lLayout = new GridLayoutManager(this,2); yourRecyclerView.setLayoutManager(lLayout);

和rest将与您在Recyclerview适配器中设置数据相同。

将您的activity_main.xml更改为此。

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

像这样修改您的MainActivity.java。

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    initComponents();
}


private void initComponents() {
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
    recyclerView.setAdapter(new GridAdapter(this));
}

class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {

    private final LayoutInflater layoutInflater;
    String[] tempButtonNames;
    Context context;

    GridAdapter(Context activity) {
        context = activity;
        Resources res = context.getResources();
        tempButtonNames = res.getStringArray(R.array.button_names);
        layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public GridAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new GridAdapter.ViewHolder(layoutInflater.inflate(R.layout.item, null));
    }

    @Override
    public void onBindViewHolder(final GridAdapter.ViewHolder viewHolder, final int position) {
        viewHolder.button.setText(tempButtonNames[position]);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return tempButtonNames.length;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        Button button;

        ViewHolder(View itemView) {
            super(itemView);
            button = (Button) itemView.findViewById(R.id.button);
        }
    }
}

}

在build.gradle

中添加
compile 'com.android.support:recyclerview-v7:25.3.0'

希望你的问题得到解决。

建议使用RecyclerView。