GridView按钮无法正常工作

时间:2017-04-20 19:11:15

标签: android xml gridview

我有一个GridView,并使用BaseAdapter填充每个网格项的值。我使用此适配器以编程方式添加每个网格项中的按钮。问题是,我总共有七个按钮。单击每个按钮后,该按钮被禁用,因此只能单击一次。

因此,当单击第二个按钮时,第7个按钮停止工作。如果单击第7个按钮,则第一个按钮停止工作。按钮工作正常,除此之外功能正常。

编辑:看起来像滚动浏览gridview(因为gridview中的元素很大会导致问题。)

以下是相关代码。

MyAdapter.java

        import static com.example.tuss.mafia.R.layout.layout_grid_item;

public class MyAdapter extends BaseAdapter {
Context context;
ArrayList<Players> playerList;
private static LayoutInflater inflater = null;

public MyAdapter(Context context, ArrayList<Players> playerList){
    this.context = context;
    this.playerList = playerList;
    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


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

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null)
        convertView = inflater.inflate(layout_grid_item, null);

    final Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
    TextView idTextView = (TextView) convertView.findViewById(R.id.tv_player_id);
    TextView nameTextView = (TextView) convertView.findViewById(R.id.tv_player_name);
    final TextView roleTextView = (TextView) convertView.findViewById(R.id.tv_player_role);

    Players p;
    p = playerList.get(position);
    idTextView.setText("ID: " + String.valueOf(p.getId()));
    nameTextView.setText(String.valueOf(p.getName()));
    roleTextView.setText("Your Role Is: " + String.valueOf(p.getRole())+ ", you won't be able to view this again.");

    final String role = roleTextView.getText().toString();

    button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            new AlertDialog.Builder(context)
                    .setTitle("Remember your role and keep it a secret!")
                    .setMessage(role)
                    .create()
                    .show();
                    button.setEnabled(false);
                    button.setText("Role has been viewed");
            return true;
        }
    });

    return convertView;
}
}

gridview的XML, fragment_viewroles.xml

<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: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="com.example.tuss.mafia.GameActivity" >



<GridView
    android:id="@+id/gv_players"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:nestedScrollingEnabled="true"
    android:columnWidth="150dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="20dp"
    android:textAlignment="center"
    android:gravity="center">
</GridView>

</RelativeLayout>

layout_grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/customborder"
android:orientation="vertical" >

<TextView
    android:id="@+id/tv_player_id"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:textColor="@android:color/white"
    android:visibility="gone"
    android:text="ID" />

<TextView
    android:id="@+id/tv_player_name"
    android:textAlignment="center"
    android:textSize="35dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@android:color/holo_blue_bright"
    android:text=""
    android:textStyle="normal|bold" />

<TextView
    android:id="@+id/tv_player_role"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAlignment="center"
    android:clickable="true"
    android:text="role"
    android:visibility="invisible">
</TextView>

<Button
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Hold to reveal your role"
    android:id="@+id/btnRoleReveal"
    />

RolesFragment.java

public RolesFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_viewroles, container, false);

    gridView = (GridView) view.findViewById(R.id.gv_players);
    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
    playersList = new ArrayList<Players>();

    playersList = databaseHelper.getPlayers();
    adapter = new MyAdapter(getActivity(), playersList);
    gridView.setAdapter(adapter);
    return view;
}

2 个答案:

答案 0 :(得分:0)

替换button.setEnabled(false); with((Button)v)。setEnabled(false);

答案 1 :(得分:0)

我认为问题出在您的按钮事件中。 只需初始化按钮事件

if (convertView == null){
    convertView = inflater.inflate(layout_grid_item, null);
    Button button = (Button) convertView.findViewById(R.id.btnRoleReveal);
    button.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        Button btn = (Button)v;
        new AlertDialog.Builder(context)
                .setTitle("Remember your role and keep it a secret!")
                .setMessage(role)
                .create()
                .show();
                btn.setEnabled(false);
                btn.setText("Role has been viewed");
        return true;
     }
    });
}

Check this KodeCenter example