Android:Master Detail流程,onItemClick()未触发

时间:2013-05-03 11:44:43

标签: android android-arrayadapter master-detail

这是我第一次尝试使用Master-Detail流程应用程序,我有点迷失了。我使用Android SDK创建虚拟项目,并创建了一个自定义ArrayAdapter来替换纯文本列表。现在每行都有一个文本字段和一个开关。屏幕上的一切都显得正常,但现在的问题是,当我点击一行时,没有任何反应。我想我错过了一些明显的东西,但我已经看了好几个小时了。我猜我在回调时做错了。但我不知道是什么!这是代码:

ServerListActivity.java:

public class ServerListActivity extends FragmentActivity implements
    ServerListFragment.Callbacks {

private boolean mTwoPane;

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

    if (findViewById(R.id.server_detail_container) != null) {
        //in two-pane mode.
        mTwoPane = true;
        ((ServerListFragment) getSupportFragmentManager().findFragmentById(
                R.id.server_list)).setActivateOnItemClick(true);
    }
}

/**
 * Callback method from {@link ServerListFragment.Callbacks} indicating that
 * the item with the given ID was selected.
 */
@Override
public void onItemSelected(String id) {
    Log.i("info", "item selected");
    if (mTwoPane) {
        Bundle arguments = new Bundle();
        arguments.putString(ServerDetailFragment.ARG_ITEM_ID, id);
        ServerDetailFragment fragment = new ServerDetailFragment();
        fragment.setArguments(arguments);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.server_detail_container, fragment).commit();

    } else {
        Intent detailIntent = new Intent(this, ServerDetailActivity.class);
        detailIntent.putExtra(ServerDetailFragment.ARG_ITEM_ID, id);
        startActivity(detailIntent);
    }
}

}

ServerListFragment.java:

public class ServerListFragment extends ListFragment {

private static final String STATE_ACTIVATED_POSITION = "activated_position";
private Callbacks mCallbacks = sDummyCallbacks;
private int mActivatedPosition = ListView.INVALID_POSITION;

public interface Callbacks {
    public void onItemSelected(String id);
}

private static Callbacks sDummyCallbacks = new Callbacks() {
    @Override
    public void onItemSelected(String id) {
    }
};

public ServerListFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setListAdapter(new ServerAdapter(getActivity(),
            R.layout.server_row, android.R.id.text1 ,
            DummyContent.ITEMS));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    Log.i("info", "List Fragment Created");
    if (savedInstanceState != null && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
        setActivatedPosition(savedInstanceState
                .getInt(STATE_ACTIVATED_POSITION));
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.i("info", "this runs");
    if (!(activity instanceof Callbacks)) {
        Log.i("info", "callbacks not found");
        throw new IllegalStateException(
                "Activity must implement fragment's callbacks.");
    }

    mCallbacks = (Callbacks) activity;
}

@Override
public void onDetach() {
    super.onDetach();

    mCallbacks = sDummyCallbacks;
}

@Override
public void onListItemClick(ListView listView, View view, int position,
        long id) {
    super.onListItemClick(listView, view, position, id);

    // Notify the active callbacks interface (the activity, if the
    // fragment is attached to one) that an item has been selected.
    Log.i("info", "this doesn't run");
    mCallbacks.onItemSelected(DummyContent.ITEMS.get(position).getIp());
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    if (mActivatedPosition != ListView.INVALID_POSITION) {
        // Serialize and persist the activated item position.
        outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
    }
}

public void setActivateOnItemClick(boolean activateOnItemClick) {
    // When setting CHOICE_MODE_SINGLE, ListView will automatically
    // give items the 'activated' state when touched.
    getListView().setChoiceMode(
            activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
                    : ListView.CHOICE_MODE_NONE);
}

private void setActivatedPosition(int position) {
    if (position == ListView.INVALID_POSITION) {
        getListView().setItemChecked(mActivatedPosition, false);
    } else {
        getListView().setItemChecked(position, true);
    }

    mActivatedPosition = position;
}

}

ServerAdapter.java

public class ServerAdapter extends ArrayAdapter<Server>{

private ArrayList<Server> objects;

public ServerAdapter(Context context, int textViewResourceId, int view2, ArrayList<Server> objects) {
    super(context, textViewResourceId, view2, objects);
    this.objects = objects;
}

public View getView(int position, View convertView, ViewGroup parent){

    View v = convertView;

    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.server_row, null);
    }


    Server i = objects.get(position);

    if (i != null) {

        TextView tt = (TextView) v.findViewById(R.id.server);

        if (tt != null){
            tt.setText(i.getIp());
        }
    }

    // the view must be returned to our activity
    return v;

}

}

1 个答案:

答案 0 :(得分:0)

原来我必须在my xml

的顶级元素中添加一个属性
android:descendantFocusability="blocksDescendants" 

显然这会阻止子元素覆盖列表项的单击侦听器。