3

我有 4 个TextViewsGridView给定的Fragment. 我的主要Activity通过 a 得到通知TimerTask以更新正确的文本颜色TextView。这适用于TextViews除第一个应用程序首次启动时之外的所有应用程序。之后,它像其他的一样正常工作TextViews

在有Fragment问题的:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.short_press_info_fragment, container, false);
    GridView grid = (GridView) view.findViewById(R.id.grid_view);
    infoAdapter = new ShortPressInfoAdapter(mKeyInfo, getActivity());
    grid.setAdapter(infoAdapter);
    return view; 
}

在适配器中:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(convertView == null) {
        Log.d("GRID_VIEW", "inflating");
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.grid_item, null);
        TextView text = texts[position] = (TextView) v.findViewById(R.id.gridItemText);
        Log.d("GRID_VIEW", "creating view");
        text.setText(mKeyInfo[position]);
        switch(position) {
        case 0:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
            break;
        case 1:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
            break;
        case 2:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.green));
            break;
        case 3:
            text.setBackgroundColor(mContext.getResources().getColor(R.color.red));
            break;
        default:
            break;
        }
    }
    return v;
}

在主要Activity

public void emphasizeShort(final PressID p, final boolean b) {
runOnUiThread(new Runnable() {
  @Override
  public void run() {
    if(b) {
      Log.d(TAG, "setting short " + p + " to black");
      getShortTextView(p).setTextColor(getResources().getColor(R.color.black));
    }
    else {
      Log.d(TAG, "setting short " + p + " to white");
      getShortTextView(p).setTextColor(getResources().getColor(R.color.white));
    }
  }
});
}

无论TextView我在应用程序启动时尝试更改哪个,日志都会向我显示相同的输出,但第一个TextView是唯一一个根本不会更改的输出。

什么会导致View应用程序启动时忽略更改而不是之后?从这段代码中,我看不出为什么要TextView挑出第一个。

任何帮助将不胜感激。

编辑:这是发送强调TextViews 的请求的函数。

@Override
public boolean onTouch(final View v, final MotionEvent event) {
  if (event.getAction() == MotionEvent.ACTION_DOWN) { // button pressed
    pressTime = System.currentTimeMillis();
    timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        Log.d("TIMER_TASK", "short press "
            + (System.currentTimeMillis() - pressTime));
        mFourButtonListener.onShortPress(buttonID);
      }
    }, SHORT_PRESS_DELAY);
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        Log.d("TIMER_TASK", "long press "
            + (System.currentTimeMillis() - pressTime));
        mFourButtonListener.onLongPress(buttonID);
      }
    }, LONG_PRESS_DELAY);
    return true;
  } else if (event.getAction() == MotionEvent.ACTION_UP) {
    duration = System.currentTimeMillis() - pressTime;
    timer.cancel();
    timer.purge();
    if (duration >= SHORT_PRESS_DELAY && duration < LONG_PRESS_DELAY)
      mFourButtonListener.onShortRelease(buttonID);
    else if (duration >= LONG_PRESS_DELAY)
      mFourButtonListener.onLongRelease(buttonID);
    return true;
  }
  return false;
}
}

我越来越绝望了。

4

2 回答 2

3

这里有很多缺失的代码,但是如果您在应用程序第一次启动时遇到它无法正常工作的问题,那么问题可能是由 引起的runOnUiThread,这在启动时根本不可靠。(即,并非每个发布的任务都能保证运行。)

添加类级Handler变量

Handler h;

并在期间创建它onCreate

h = new Handler();

然后使用h.post()而不是runOnUiThread.

于 2013-11-28T21:26:09.987 回答
2

如果不。of TextViews 将保持 4,无论如何都不要使用GridView. 改为使用LineareLayout。无论如何,如果您添加更多TextViews,您当前的方法将停止工作。乍一看你getView()是有问题的,你必须这样做:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(convertView == null) {
        Log.d("GRID_VIEW", "inflating");
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.grid_item, null);
    } // <<<<------------------- 
    TextView text = (TextView) v.findViewById(R.id.gridItemText);
    Log.d("GRID_VIEW", "creating view");
    text.setText(mKeyInfo[position]);
    switch(position) {
    case 0:
        text.setBackgroundColor(mContext.getResources().getColor(R.color.blue));
        break;
    case 1:
        text.setBackgroundColor(mContext.getResources().getColor(R.color.yellow));
        break;
    case 2:
        text.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        break;
    case 3:
        text.setBackgroundColor(mContext.getResources().getColor(R.color.red));
        break;
    default:
        break;
    } 
    return v;
}

getView()基本上,由于View回收,每次调用时都必须执行所有与状态相关的操作。

顺便说一句,为什么您要在texts数组中保留对自己的引用?这是非常非常糟糕的做法。你为什么需要这个?

如果您想在后期更改颜色,那么您需要更改整个逻辑。你需要做类似这样的事情:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class MyAdapter extends BaseAdapter {

int[] colors = new int[4];

public MyAdapter(Context context) {
    colors[0] =  context.getResources().getColor(R.color.blue);
    colors[1] =  context.getResources().getColor(R.color.yellow);
    colors[2] =  context.getResources().getColor(R.color.green);
    colors[3] =  context.getResources().getColor(R.color.red);
}

@Override
public int getCount() { 
    return colors.length;
}

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

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

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if(convertView == null) {
        Log.d("GRID_VIEW", "inflating");
        LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = li.inflate(R.layout.grid_item, null);
    } // <<<<------------------- 
    TextView text = (TextView) v.findViewById(R.id.gridItemText);
    Log.d("GRID_VIEW", "creating view");
    text.setText(mKeyInfo[position]);
    text.setBackgroundColor(colors[position]); 
    return v;
}
// call this method to change colors at later stage
public void changeColor(int position, int color){
    colors[position] = color;
    notifyDataSetChanged();
}

// call this method to change text at later stage
public void changeText(int position, String text){
    mKeyInfo[position] = text;
    notifyDataSetChanged();
}
}
于 2013-11-29T06:52:43.007 回答