-1

我试图做一个ListFragment。我看了Api Demo(FragmentLayout)。它适用于一个简单的例子,现在我想将它应用到我现有的项目中。与listFragment自定义arrayAdapter

这是我的代码。我在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个textViews和一个可点击的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

EDIT =目前我制造“showRecipeDetails”静态和调用它在我的自定义ArrayAdapter听众。

convertView.setOnClickListener(new OnClickListener() { 

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

我不满意我的解决方案,我interessed任何其他解决方案

回答

1

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

+0

Thx为答案,但它不起作用。使用我的培训示例(http://android-developers.blogspot.fr/2011/02/android-30-fragments-api.html),我不需要将列表与OnItemClickListener相关联。 使用自定义ArrayAdapter导致一些问题 – yoann 2012-08-06 15:24:04

+0

这个答案适合我,谢谢Espiandev – Redshirt 2013-03-11 00:12:07

相关问题