本文介绍了无法启用/禁用内部GridView控件可点击的TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可点击的TextViews为GridView的项目,使用适配器(该TextViews作为按键)的GridView。当我点击一个TextView,我想要禁用遍布在GridView TextViews - 禁止在这个意义上,用户无法点击他们了。我所做的是这样的:

 私人无效toggleButtonsState(启用布尔)
    {
        的for(int i = 0; I< this.gridview.getChildCount();我++)
        {
            this.gridview.getChildAt(I).setEnabled(启用);
        }
    }

然而,即使这code被称为启用== false时,按钮仍然启用。什么错在这里?我怎样才能得到其他TextViews禁用?谢谢!


我没有你所说的话,并直接实施BaseAdapter。然而,再这2种方法永远不会叫:既没有开头也没有在notifyDataSetChanged。真不知道是怎么回事。这里是code:

 公共类TextViewAdapter扩展BaseAdapter实现View.OnClickListener {@覆盖
公共布尔areAllItemsEnabled(){
    // TODO自动生成方法存根
    返回super.areAllItemsEnabled();
}@覆盖
公共布尔isEnabled(INT位置){
    // TODO自动生成方法存根
    返回super.isEnabled(位置);
}受保护的语境mContext;公共TextViewAdapter(上下文C)
{
    this.mContext = C;
}@覆盖
公众诠释的getCount(){
    // TODO自动生成方法存根
    返回DbManager.getInstance(mContext).getCategoryCount(真);
}@覆盖
公共对象的getItem(INT为arg0){
    // TODO自动生成方法存根
    返回将arg0;
}
@覆盖
众长getItemId(INT为arg0){
    // TODO自动生成方法存根
    返回0;
}@覆盖
公共查看getView(INT为arg0,查看convertView,ViewGroup中ARG2){    最终的LinearLayout的LinearLayout;    如果(convertView == NULL){//如果它没有回收,初始化一些属性        LayoutInflater吹气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        的LinearLayout =(的LinearLayout)inflater.inflate(R.layout.settings_timer_textview,NULL);
        TextView中的TextView =(TextView中)linearLayout.findViewById(R.id.timer_textview);
        textView.setOnClickListener(本);    }其他{
        的LinearLayout =(LinearLayout中)convertView;
    }
    setCategoryView(LinearLayout中,将arg0);
    setControls(LinearLayout中,将arg0);    返回的LinearLayout;}私人无效setControls(LinearLayout中的LinearLayout,INT指数)
{
    TextView中的TextView =(TextView中)linearLayout.findViewById(R.id.timer_textview);
    分类分类=的getCategory(指数);
    textView.setTag(类);
    textView.setText(category.getName());
    textView.setTextColor(TimeManagerActivity.getForeColorByBrightness(category.getColor()));
    textView.setBackgroundDrawable(createTextViewDrawable(category.getColor()));    复选框复选框=(复选框)linearLayout.findViewById(R.id.timer_checkbox);
    checkbox.setChecked(category.getEnabled()!= 0);
    checkbox.setTag(textView.getText()的toString());
}保护类别的getCategory(INT指数)
{
    返回DbManager.getInstance(this.mContext).getCategoryByIndex(指数,真正的);
}保护无效setCategoryView(查看视图,INT指数)
{
    // TODO自动生成方法存根    TextView中的TextView =(TextView中)view.findViewById(R.id.timer_textview);
    textView.setGravity(Gravity.CENTER);
    textView.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,40));    复选框复选框=(复选框)view.findViewById(R.id.timer_checkbox);
    checkbox.setVisibility(View.GONE);
}@覆盖
公共无效的onClick(查看arg0中)
{
    // TODO自动生成方法存根
    this.notifyDataSetChanged();
}公共无效更新()
{
    this.notifyDataSetChanged();
}私人StateListDrawable createTextViewDrawable(INT颜色)
{
    //创建按钮的渐变。身高是很难coded到30(我不知道事先的高度)。
    //我希望我能设置对齐按钮底部的梯度。
    最终着色着色=新的LinearGradient(0,0,0,0,
        新的INT [] {颜色,颜色},
        空,Shader.TileMode.MIRROR);    浮动[] = roundedCorner新的浮动[] {5,5,5,5,5,5,5,5};
    ShapeDrawable正常=新ShapeDrawable(新RoundRectShape(roundedCorner,NULL,NULL));    normal.getPaint()setShader(着色器)。
    normal.setPadding(3,3,3,3);    //创建一个国家列表(pressed我燮pressed设置)。
    StateListDrawable state_list =新StateListDrawable();
    state_list.addState(新INT [] {},正常);    返回state_list;
}

}


下面是 TextViewAdapter 派生我的类本身是由 BaseAdapter 导出。我的 TextViewAdapter 只是定义了一些抽象方法和没有做太多。在 TextViewApaterMain 尝试停用网格中的所有TextViews除了被点击的人。在的OnClick 我点击一个TextView并要禁用所有其他TextViews,所以我称之为 notifyDataSetChanged ,但覆盖 areAllItemsEnabled isEnabled 不叫(调试器不会在断点处停止)。有任何想法吗?下面是一些code:

 公共类TextViewAdapterMain扩展TextViewAdapter {私人的ArrayList<布尔> textViewStates =新的ArrayList<布尔>();公共TextViewAdapterMain(上下文C){
    超(C);
    // TODO自动生成构造函数存根
}@覆盖
公众诠释的getCount(){
    // TODO自动生成方法存根
    返回DbManager.getInstance(mContext).getCategoryCount(真);
}@覆盖
保护无效setCategoryView(查看视图,INT指数){
    // TODO自动生成方法存根    TextView中的TextView =(TextView中)view.findViewById(R.id.timer_textview);
    textView.setGravity(Gravity.CENTER);
    textView.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,40));    复选框复选框=(复选框)view.findViewById(R.id.timer_checkbox);
    checkbox.setVisibility(View.GONE);
}@覆盖
公共无效的onClick(查看为arg0){
    // TODO自动生成方法存根
    super.onClick(为arg0);    ((TimeManagerActivity)super.mContext).launchChronometer((TextView中)为arg0);
    this.notifyDataSetChanged();
}@覆盖
保护类别的getCategory(INT指数){
    // TODO自动生成方法存根
    返回DbManager.getInstance(this.mContext).getCategoryByIndex(指数,真正的);
}@覆盖
公共布尔areAllItemsEnabled(){
    返回false;
}@覆盖
公共布尔isEnabled(INT位置)
{
    如果(textViewStates.get(位置))
        返回true;    返回false;}}


解决方案

这可能不会工作。你需要做的是覆盖你BaseAdapter类两种功能,像这样:

  @覆盖
公共布尔areAllItemsEnabled(){
    返回false;
}@覆盖
公共布尔isEnabled(INT位置){
    如果(isEnabledData [POS])
        返回true;    返回false;
}

这也意味着你将需要添加的方式将数据/模型跟踪选择状态,在我的例子,我用一个简单的布尔[]与真或假。该BaseAdapter将使用isEnabled当它绘制网格项目在屏幕上。

如果您需要更改项目的状态,切换到模型数据isEnable [X] =真/假,然后在适配器上调用notifyDatasetChanged()。

I have a GridView with clickable TextViews as GridView items, using an Adapter (the TextViews serve as Buttons). When I click on a TextView, I want to disable all over TextViews in the GridView - disable in the sense that the user cannot click them anymore. What I did is this:

    private void toggleButtonsState(boolean enabled)
    {
        for (int i = 0; i < this.gridview.getChildCount(); i++)
        {
            this.gridview.getChildAt(i).setEnabled(enabled);
        }
    }

However, even though this code is called with enabled == false, the buttons are still enabled. What wrong here? How can I get the other TextViews disabled? Thanks!


I did what you said and implemented BaseAdapter directly. However, again these 2 methods never get called: neither at the beginning nor upon notifyDataSetChanged. Really don't know what's going on. Here is the code:

public class TextViewAdapter extends BaseAdapter implements View.OnClickListener  {

@Override
public boolean areAllItemsEnabled() {
    // TODO Auto-generated method stub
    return super.areAllItemsEnabled();
}

@Override
public boolean isEnabled(int position) {
    // TODO Auto-generated method stub
    return super.isEnabled(position);
}

protected Context mContext;

public TextViewAdapter(Context c)
{
    this.mContext = c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return DbManager.getInstance(mContext).getCategoryCount(true);
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}


@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {

    final LinearLayout linearLayout;

    if (convertView == null) {  // if it's not recycled, initialize some attributes

        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        linearLayout = (LinearLayout)inflater.inflate(R.layout.settings_timer_textview, null);
        TextView textView = (TextView)linearLayout.findViewById(R.id.timer_textview);
        textView.setOnClickListener(this);

    } else {
        linearLayout = (LinearLayout) convertView;
    }


    setCategoryView(linearLayout, arg0);
    setControls(linearLayout, arg0);

    return linearLayout;

}

private void setControls(LinearLayout linearLayout, int index)
{
    TextView textView = (TextView)linearLayout.findViewById(R.id.timer_textview);
    Category category = getCategory(index);
    textView.setTag(category);
    textView.setText(category.getName());
    textView.setTextColor(TimeManagerActivity.getForeColorByBrightness(category.getColor()));
    textView.setBackgroundDrawable(createTextViewDrawable(category.getColor()));

    CheckBox checkbox = (CheckBox)linearLayout.findViewById(R.id.timer_checkbox);
    checkbox.setChecked(category.getEnabled() != 0);
    checkbox.setTag(textView.getText().toString());
}

protected Category getCategory(int index)
{
    return DbManager.getInstance(this.mContext).getCategoryByIndex(index, true);
}

protected void setCategoryView(View view, int index)
{
    // TODO Auto-generated method stub

    TextView textView = (TextView)view.findViewById(R.id.timer_textview);
    textView.setGravity(Gravity.CENTER);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 40));

    CheckBox checkbox = (CheckBox)view.findViewById(R.id.timer_checkbox);
    checkbox.setVisibility(View.GONE);
}

@Override
public void onClick(View arg0)
{
    // TODO Auto-generated method stub
    this.notifyDataSetChanged();
}

public void update()
{
    this.notifyDataSetChanged();
}

private StateListDrawable createTextViewDrawable(int color)
{
    // Create a gradient for the button. Height is hardcoded to 30 (I don't know the height beforehand).
    // I wish I could set the gradient aligned to the bottom of the button.
    final Shader shader = new LinearGradient(0, 0, 0, 0,
        new int[] { color, color },
        null, Shader.TileMode.MIRROR);

    float[] roundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));

    normal.getPaint().setShader(shader);
    normal.setPadding(3, 3, 3, 3);

    // Create a state list (I suppressed settings for pressed).
    StateListDrawable state_list = new StateListDrawable();
    state_list.addState(new int[] { }, normal);

    return state_list;
}

}


Here is my class derived from TextViewAdapter which itself is derived from BaseAdapter. My TextViewAdapter just defines some abstract methods and doesn't do much. The TextViewApaterMain tries to disable all TextViews in the Grid except for the one that was clicked on. In OnClick I click a TextView and want to disable all other TextViews, so I call notifyDataSetChanged, but the overrides areAllItemsEnabled and isEnabled are not called (debugger doesn't stop at breakpoint). Any ideas? Here's some code:

public class TextViewAdapterMain extends TextViewAdapter  {

private ArrayList<Boolean> textViewStates = new ArrayList<Boolean>();

public TextViewAdapterMain(Context c) {
    super(c);


    // TODO Auto-generated constructor stub
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return DbManager.getInstance(mContext).getCategoryCount(true);
}

@Override
protected void setCategoryView(View view, int index) {
    // TODO Auto-generated method stub

    TextView textView = (TextView)view.findViewById(R.id.timer_textview);
    textView.setGravity(Gravity.CENTER);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 40));

    CheckBox checkbox = (CheckBox)view.findViewById(R.id.timer_checkbox);
    checkbox.setVisibility(View.GONE);
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    super.onClick(arg0);

    ((TimeManagerActivity)super.mContext).launchChronometer((TextView)arg0);


    this.notifyDataSetChanged();
}

@Override
protected Category getCategory(int index) {
    // TODO Auto-generated method stub
    return DbManager.getInstance(this.mContext).getCategoryByIndex(index, true);
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position)
{
    if(textViewStates.get(position))
        return true;

    return false;

}

}
解决方案

This probably won't work. What you need to do is override two functions in your BaseAdapter class like so:

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    if(isEnabledData[pos])
        return true;

    return false;
}

This also means you will need add a way to your data/model to keep track of selection state, in my example I use a simple boolean[] with true or false. The BaseAdapter will use isEnabled when it draws the grid items to the screen.

If you need to change the state of an item, change to model data isEnable[x] = true/false and then call notifyDatasetChanged() on the adapter.

这篇关于无法启用/禁用内部GridView控件可点击的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:06