-1

ListFragment を作成しようとしています。Api Demo (FragmentLayout) を見ました。簡単な例で動作し、既存のプロジェクトに適用したいと考えています。

これが私のコードです。Api Demo のように内部クラス (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 が含まれています)。

AccountListAdapter :

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 サービスを消費するのは適切な方法ですか (リストにデータを入力するため)?

事前にThx

編集= とりあえず、「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 に答える