-1

我正在尝试制作一个 ListFragment。我看了 Api Demo (FragmentLayout)。它适用于一个简单的示例,现在我想将它应用到我现有的项目中。

这是我的代码。我在 Api 演示中创建内部类(RecipeList 和 RecipeDetail)。

public class InfoActivity extends MenuActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info_fragment_layout);

    // ...


}

    public static class RecipeList extends ListFragment {

    private int mCurrentSelectedItemIndex = -1;
    private boolean mIsTablet = false;

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



        accountData = new ArrayList<Account>();


        new AccountSyncTask() {
            @Override
            public void onPostExecute(
                    final ArrayList<ArrayList<String>> result) {

                    // For each retrieved account   
                                            Bd.insert(retrievedAccount);
                    accountData.add(retrievedAccount);
                }

                accountListAdapter = new AccountListAdapter(
                        InfoActivity.this, R.layout.accountlist_detail,
                        accountData);

                accountListAdapter = new AccountListAdapter(
                        activityContext, R.layout.accountlist_detail,
                        accountData);
                setListAdapter(accountListAdapter);


            }
        }.execute(sessionName, null, "getAllObjectOnServer",
                String.valueOf(nbRow));


        if (savedInstanceState != null) {
            mCurrentSelectedItemIndex = savedInstanceState.getInt(
                    "currentListIndex", -1);
        }

        // This is a tablet if this view exists
        View recipeDetails = getActivity()
                .findViewById(R.id.recipe_details);
        mIsTablet = recipeDetails != null
                && recipeDetails.getVisibility() == View.VISIBLE;


        if (mIsTablet) {
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        }


        if (mIsTablet && mCurrentSelectedItemIndex != -1) {
            showRecipeDetails();
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        mCurrentSelectedItemIndex = position;
        showRecipeDetails();
    }

    private void showRecipeDetails() {
        if (mIsTablet) {

            // Set the list item as checked
            getListView().setItemChecked(mCurrentSelectedItemIndex, true);

            // Get the fragment instance
            RecipeDetail details = (RecipeDetail) getFragmentManager()
                    .findFragmentById(R.id.recipe_details);
            // Is the current visible recipe the same as the clicked? If so,
            // there is no need to update
            if (details == null
                    || details.getRecipeIndex() != mCurrentSelectedItemIndex) {

                details = RecipeDetail
                        .newInstance(mCurrentSelectedItemIndex);

                FragmentTransaction ft = getFragmentManager()
                        .beginTransaction();
                ft.replace(R.id.recipe_details, details);

                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
        } else {

        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("currentListIndex", mCurrentSelectedItemIndex);
    }

}

public static class RecipeDetail extends Fragment {

    private int mRecipeIndex;

    public static RecipeDetail newInstance(int recipeIndex) {
        // Create a new fragment instance
        RecipeDetail detail = new RecipeDetail();
        // Set the recipe index
        detail.setRecipeIndex(recipeIndex);
        return detail;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            return null;
        }


        View v = inflater
                .inflate(R.layout.recipe_details, container, false);
        //..

        return v;
    }

    public int getRecipeIndex() {
        return mRecipeIndex;
    }

    public void setRecipeIndex(int index) {
        mRecipeIndex = index;
    }

}

我有一个自定义的 ArrayAdapter(我在 ListFragment 中的项目包含 4 个 textView 和一个可点击的 imageButton)。

帐户列表适配器:

public class AccountListAdapter extends ArrayAdapter<Account> {

private final Context context;
private final int layoutResourceId;
private final ArrayList<Account> data;


public AccountListAdapter(Context context, int layoutResourceId,
        ArrayList<Account> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;


}



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

    AccountHolder holder = null;

    if (convertView == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        convertView = inflater.inflate(layoutResourceId, parent, false);

        holder = new AccountHolder();

        convertView.setClickable(true);
        convertView.setFocusable(true);

        holder.txtName = (TextView) convertView.findViewById(R.id.nom);
        holder.txtId = (TextView) convertView.findViewById(R.id.id);

        convertView.setTag(holder);

    } else {
        holder = (AccountHolder) convertView.getTag();
    }

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.i("click", "index = " + position);

        }

    });

    holder.txtName.setText(data.get(position).getName());
    holder.txtId.setText(data.get(position).getId());

    convertView.setBackgroundResource(R.drawable.list_selector);

    ImageButton img = (ImageButton) convertView.findViewById(R.id.check);
    img.setTag(position);

    return convertView;
}

static class AccountHolder {
    TextView txtName;
    TextView txtId;
}

}

问题:当我点击 listFragment 的一个项目时,

public void onListItemClick(ListView l, View v, int position, long id) {
        mCurrentSelectedItemIndex = position;
                    Log.i("click", "here";
        showRecipeDetails();
    } 

未调用,但 AccountListAdapter 中定义的项目上的侦听器有效

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            Log.i("click", "index = " + position);

        }

    });

为什么从不调用 onListitemClick ?

另一个问题:在我的 ListFragment 的 onActivityCreated 函数中的另一个线程中使用 Web 服务是否是一种正确的方法(以填充列表)?

提前谢谢

编辑= 目前我将“showRecipeDetails”设为静态并在我的自定义 ArrayAdapter 的侦听器中调用它。

convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            RecipeList.showRecipeDetails(position);
}}

我对我的解决方案不满意,我对任何其他解决方案都很感兴趣

4

1 回答 1

1

OnItemClickListeners 必须首先与您要为其记录点击的 ListView 相关联。在您的onActivityCreated(..)方法中,放置在getListView().setOnItemClickListener(this)某处并放在implements OnItemClickListener之后public static class RecipeList extends ListFragment

于 2012-08-06T14:26:26.020 回答