Android GridView selector not working. Also how to get gridlines for specific columns in GridView?

时间:2017-04-12 05:18:08

标签: java android android-layout gridview

I am building a Sudoku app on android. I want to populate the screen with 81 textViews. I am using a GridView for that. Some how my selector is not working. I have defined two xmls for background of text view. One for selected and unselected. I have a selector xml which I have called in the Gridview xml. But my text views are not selectable when i click on them.

Here are my xmls: selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
<solid android:color="#ffffff" />
<stroke android:width="4dp" android:color="#4f7df2"/>
</shape>

unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
    <solid android:color="#ffffff" />
    <stroke android:width="0.5dp" android:color="#9C7F7F"/>
</shape>

selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selected"
    android:state_selected="true"/>
<item android:drawable="@drawable/unselected"/>
</selector>

Also I want to further divide this into 9 grids. I want to divide it using grid lines so its evident to the user which grid he is in. How do I achieve that goal?

Here is my SudokuAdapter class

public class SudokuAdapter extends BaseAdapter {
private Context context;

public SudokuAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return 81;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    TextView textView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        textView = new TextView(context);
        textView.setBackground(context.getDrawable(R.drawable.unselected));
        textView.didTouchFocusSelect();
        textView.requestFocus();
        textView.setLayoutParams(new GridView.LayoutParams(60, 60));
    } else {
        textView = (TextView) convertView;
    }

    return textView;
}
}

I tried removing setBackground from above, then the selector working only for selecting the textview. I want the default background if not selected to be unselected.

Here is my activity:

public class TestActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sudoku_grid_layout);

    GridView gridView = (GridView) findViewById(R.id.sudoku_grid);
    gridView.setAdapter(new SudokuAdapter(this));

} 
}

Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sudoku_grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="9"
android:listSelector="@drawable/selector"
android:gravity="center"
/>

2 个答案:

答案 0 :(得分:0)

您甚至不必使用适配器使其可选。您的数据集不会更改。只需使用表格布局并循环遍历单元格。

要使textview可选,请使用此选项。

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        textView.setSelected(true);
    }
}

答案 1 :(得分:0)

您可以通过

执行此操作
textView.setOnClickClickListener(new View.OnClickListener() {
private boolean stateChanged;
public void onClick(View view) {
    if(stateChanged) {
        // reset background to default;
        textView.setBackgroundDrawable(circleOffDrawable);
    } else {
        textView.setBackgroundDrawable(circleOnDrawable);
    }
    stateChanged = !stateChanged;
 }
});

您必须保持所选状态或未选中状态 你的适配器中的也会改变setBackground的行

textView.setBackground(context.getDrawable(R.drawable.selector));

由于选择器具有这两个属性,因此它会自动为您更改。