当点击gridview项时,如何在正确的项目中添加片段

时间:2016-11-14 13:15:52

标签: android gridview fragment

这是我的CategoryFragment:

public class CategoryFragment extends Fragment implements    CategoriesItemsAdapter.CategoriesDelegate {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    mView = inflater.inflate(R.layout.fragment_budget,container,false);

    mSavedInstanceState= savedInstanceState;

    return mView;

}

private void setGridViewAdapter(ArrayList<String> texts, ArrayList<Integer> icons){

    mCategories = (GridView) mView.findViewById(R.id.categories);

    CategoriesItemsAdapter adapter = new CategoriesItemsAdapter(getActivity(),texts,icons);

    adapter.CategoriesDelegate = this;

    mCategories.setAdapter(adapter);

}

@Override
public void categoriesDelegate() {

    MainActivity activity = (MainActivity) getActivity();

    FragmentManager manager = activity.getSupportFragmentManager();

    FragmentTransaction transaction = manager.beginTransaction();

    if (mSavedInstanceState == null) {

        transaction.replace(R.id.categories_frame, new CategoriesFragment());

    }

    transaction.addToBackStack(null);

    transaction.commit();

}

}

这是我的CategoriesItemsAdapter:

public class CategoriesItemsAdapter extends BaseAdapter implements  View.OnClickListener {

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

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.item_categories, parent, false);

    }

    ImageView icon = (ImageView) convertView.findViewById(R.id.icon);

    TextView text = (TextView) convertView.findViewById(R.id.category_name);

    text.setText(mTexts.get(position));

    icon.setImageResource(mIcons.get(position));

    icon.setOnClickListener(this);

    return convertView;

}

@Override
public void onClick(View v) {

    if (CategoriesDelegate != null) {

        CategoriesDelegate.categoriesDelegate();

    }

}

public interface CategoriesDelegate {

    void categoriesDelegate();

}


}

我想在点击网格视图项时打开一个片段。我可以打开一个片段,但片段在错误的位置打开。片段始终在第一个项目中打开。我在该网格视图项中单击了哪个网格视图项,应该打开片段。我怎么能解决这个问题?

这是我的items_categories.xml:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<!-- Icon -->

<ImageView
    android:id="@+id/icon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    />

<FrameLayout
    android:id="@+id/categories_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"

    >

    <!-- Category Name -->

    <TextView
        android:id="@+id/category_name"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_gravity="center"
        android:gravity="center"

        />

</FrameLayout>

0 个答案:

没有答案