如何显示可扩展列表导航而不是水平导航?

时间:2014-03-05 20:45:31

标签: android android-listview android-fragments android-fragmentactivity apk-expansion-files

我目前在我的代码中使用常规水平导航,这样当您单击按钮时,它将水平导航到新的片段。我想这样做,而不是调用一个新的片段,它只是在点击/再次点击时展开/折叠。我到目前为止的代码是: 这是活动:

public class ManageNewsCategoriesActivity extends AbsBaseDoubleButtonActivity {

private List<AdapterRow> mBreakingViews;

public HashSet<CategoryCheckableRow> mCategoriesMap = new HashSet<CategoryCheckableRow>();
private int mTitleId = R.string.title_manage;

public static void newInstance(final Activity activity) {
    final Intent intent = new Intent(activity.getApplicationContext(), ManageNewsCategoriesActivity.class);
    activity.startActivity(intent);
}

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

    overridePendingTransition(R.anim.slide_in_from_bottom, R.anim.no_animation);

    mActiveFragment = ManageNewsCategoriesFragment.newInstance(getSupportFragmentManager(), 0);


}

public void animateTitle(final int textViewId) {
    animateTitle(textViewId, R.anim.slide_in_from_right);
}

public void animateTitle(final int textViewId, final int animationId) {
    final TextView titleView = (TextView) findViewById(textViewId);
    final Animation animation = AnimationUtils.loadAnimation(this, animationId);
    titleView.startAnimation(animation);
}

public void setBreakingViews(final List<AdapterRow> categoryRows) {
    mBreakingViews = categoryRows;
}

public List<CategoryCheckableRow> getBreakingViews() {
    return removeHeaders(mBreakingViews);
}

public List<AdapterRow> getBreakingViewsCategoryRows() {
    if (mBreakingViews == null) {
        return null;
    }

    return AbsBaseManageNewsFragment.getAClone(mBreakingViews);
}



private List<CategoryCheckableRow> removeHeaders(final List<AdapterRow> mCategoryRows) {
    final List<CategoryCheckableRow> items = new ArrayList<CategoryCheckableRow>();
    if (mCategoryRows != null) {
        for (final AdapterRow row : mCategoryRows) {
            if (row instanceof CategoryCheckableRow) {
                final CategoryCheckableRow item = (CategoryCheckableRow) row;
                items.add(item);
            }
        }
    }
    return items;
}

public boolean isDoneButtonEnabled() {
    return !mCategoriesMap.isEmpty();
}

public void updateCategoriesMap(final HashSet<CategoryCheckableRow> categoryRows) {
    for (final CategoryCheckableRow row : categoryRows) {
        if (!mCategoriesMap.add(row)) {
            mCategoriesMap.remove(row);
        }
    }
}

@Override
protected int getTitleId() {
    return mTitleId;
}

@Override
public void updateTitleId(final int titleId) {
    mTitleId = titleId;
    updateTitleId();
}

这是片段:

public class ManageNewsCategoriesFragment extends Fragment implements OnClickListener,OnCheckedChangeListener {
    public final static String TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT = "ManageNewsCategoriesFragment";

    public static final String TYPE = "Type";

    public static final String BREAKINGVIEWS = "Breakingviews";
    public static final String ANIMATION = "animation";

    protected Button mPreferencesDoneButton;
    public static ManageNewsCategoriesFragment newInstance(final FragmentManager manager, final int animation) {
        final ManageNewsCategoriesFragment fragment = new ManageNewsCategoriesFragment();
        final Bundle arguments = new Bundle();
        arguments.putInt(ANIMATION, animation);
        fragment.setArguments(arguments);
        final FragmentInfo fragmentInfo = new FragmentInfo(TransactionMethods.ADD, R.id.manage_news_categories_container);
        fragmentInfo.setFragmentTag(TAG_MANAGE_NEWS_CATEGORIES_FRAGMENT);
        FragmentStackManager.getInstance().transitionFragment(manager, fragment, fragmentInfo);
        return fragment;
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_manage_news_categories, container, false);
        final Bundle arguments = getArguments();
        final int animation = arguments.getInt(ANIMATION, 0);
        final ManageNewsCategoriesActivity activity = (ManageNewsCategoriesActivity) getActivity();
        if (animation != 0) {
            activity.animateTitle(R.id.actionbar_title, arguments.getInt(ANIMATION, 0));
        }
        return view;
    }
    protected void setupClickListeners() {
        mPreferencesDoneButton = (Button) getActivity().findViewById(R.id.button_done);
        Typeface face = Typeface.createFromAsset(mPreferencesDoneButton.getContext().getAssets(),
                "fonts/proxima-nova-regular.ttf");
        mPreferencesDoneButton.setTypeface(face);
        mPreferencesDoneButton.setOnClickListener(this);
        mPreferencesDoneButton.setEnabled(((ManageNewsCategoriesActivity) getActivity()).isDoneButtonEnabled());
    }
    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final TextView titleView = (TextView) getActivity().findViewById(R.id.actionbar_title);
        titleView.setText(R.string.title_manage);

        initManageNewsCategoriesFragment();

    }

    private void initManageNewsCategoriesFragment() {

        setupClickListeners();

        final Button breakingViewsButton = (Button) getView().findViewById(R.id.button_breakingviews);
        breakingViewsButton.setOnClickListener(this);

    }

    @Override
    public void onClick(final View view) {
        final ManageNewsCategoriesActivity activity = (ManageNewsCategoriesActivity) getActivity();

        switch (view.getId()) {


            case R.id.button_breakingviews:
                ManageBreakingViewsFragment.newInstance(getFragmentManager());
                return;



            case R.id.button_done:
                syncNewsCategories(activity);
                break;

            default:
                break;
        }

        activity.onBackPressed();
    }

    private void syncNewsCategories(final ManageNewsCategoriesActivity activity) {

        final List<CategoryCheckableRow> breakingViews = activity.getBreakingViews();

        if ( ArrayUtils.isNotEmpty(breakingViews)) {
            RestService.start(activity, new UserCategoriesSyncOperation(NewsContentProvider.USER_CATEGORIES_URI, breakingViews));
        }


        }
    }

这是相应的xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/altercolor2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/altercolor2"
        android:orientation="vertical" >
      <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
         <Button
                android:id="@+id/button_breakingviews"
                android:layout_width="match_parent"
                android:layout_height="@dimen/manage_news_btn_ht"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:background="@drawable/manage_market_category_btnbg"
                android:gravity="left|center_vertical"
                android:paddingLeft="@dimen/frequent_padding_left"
                android:text="@string/button_breakingviews"
                android:textColor="#cccccc"
                android:textSize="@dimen/title" />



            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:paddingRight="17.5dp"
                android:paddingTop="0dp"
                android:src="@drawable/arrow_chevron_selector" />
        </RelativeLayout>
      </LinearLayout>

</FrameLayout>

    Any clue how to go about the same? Please explain programmatically and with respect to my code to avoid confusion.

谢谢!

1 个答案:

答案 0 :(得分:0)

我认为你想要的是一个自定义的可扩展列表视图。这是一个非常好的教程:ExpandableListView