從開源庫ActionsContentView提取效果

來源:互聯網
上載者:User

https://github.com/StevenRudenko/ActionsContentView

package com.example.androidfragmenttest;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;/** * @author delvikCoder 基類,方法重用可以放在這裡 */public abstract class BaseFragment extends Fragment {private FragmentActivity activity;public View childMainView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);activity = (FragmentActivity) getActivity();childMainView = activity.getLayoutView(getLayoutId());createChildView(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return LayoutView;}public abstract void createChildView(Bundle savedInstanceState);public abstract int getLayoutId();}


public abstract void createChild(Bundle savedInstanceState);public abstract int getLayoutId();

但是這樣做有個缺點,在切換的時候,子Fragment的視圖總是在重新載入,如果操作過多會導致UI卡頓,因此這不是個好方法。


<shared.ui.actionscontentview.ActionsContentView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res/com.delvik.myprojectqiu"    android:id="@+id/actionsContentView"    android:layout_width="match_parent"    android:layout_height="match_parent"    app:actions_layout="@layout/effect_actions"    app:actions_spacing="0dp"    app:content_layout="@layout/main_content"    app:effect_actions="@anim/effect_replace_actions"    app:effect_content="@anim/effect_replace_content"    app:fade_max_value="@integer/default_pref_fade_max_value"    app:fade_type="both"    app:fling_duration="@integer/default_pref_fling_delay"    app:spacing="0dp"    app:swiping_type="full" />

 app:actions_layout="@layout/effect_actions"
這個是菜單列表的layout,很簡單的布局,一個標題一個菜單listview

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/actions_bg" >    <include        android:id="@+id/title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        layout="@layout/category_list_item" />    <ListView        android:id="@+id/actions"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_alignParentBottom="true"        android:layout_below="@id/title"        android:cacheColorHint="@color/actions_bg"        android:divider="@null"        android:listSelector="@drawable/item_background_holo_light" /></RelativeLayout>


  app:content_layout="@layout/main_content"
這個是Fragment的父容器。在裡面可以添加頭部標題列,或者更多操作。

<?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:background="@android:color/white"    android:orientation="vertical" >    <FrameLayout        android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="fill_parent" >    </FrameLayout></LinearLayout>


其中需要注意的是,使用自訂屬性。、com.delvik.myprojectqiu 是你的包名。
 xmlns:app="http://schemas.android.com/apk/res/com.delvik.myprojectqiu"

再說說菜單列表適配器類MenuAdapter

package com.delvik.adapter;import android.content.Context;import android.content.res.Resources;import android.content.res.TypedArray;import android.graphics.drawable.Drawable;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.TextView;import com.delvik.myprojectqiu.R;/** * 主菜單適配器 */public class MenuAdapter extends BaseAdapter {private LayoutInflater mInflater;private String[] mTitles;// 這個是菜單的標題private int mLayoutId;// 這個專門存放布局的public String[] getmTitles() {return mTitles;}public MenuAdapter(Context context) {mInflater = LayoutInflater.from(context);mLayoutId = R.layout.effect_replace;final Resources res = context.getResources();mTitles = res.getStringArray(R.array.effects_name);// final TypedArray layoutsArray = res// .obtainTypedArray(R.array.effect_layouts);// 這是幾個畫面的布局// final int count = layoutsArray.length();/* * mLayouts = new int[count]; for (int i = 0; i < count; ++i) { * mLayouts[i] = layoutsArray.getResourceId(i, 0); } * layoutsArray.recycle();// TypedArray這個用了之後一定要注意回收 */}public String getEffectTitle(int position) {return mTitles[position];}@Overridepublic int getCount() {return mTitles.length;// return mLayouts.length;}@Overridepublic Integer getItem(int position) {return mLayoutId;// return mLayouts[position];}// public Integer getItem() {// return mLayoutId;// }@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {final ViewHolder holder;if (convertView == null) {convertView = mInflater.inflate(R.layout.action_list_item, parent,false);holder = new ViewHolder();holder.text = (TextView) convertView.findViewById(R.id.actionlistitem);final Drawable icon = convertView.getContext().getResources().getDrawable(R.drawable.ic_action_effects);icon.setBounds(0, 0, icon.getIntrinsicWidth(),icon.getIntrinsicHeight());holder.text.setCompoundDrawables(icon, null, null, null);holder.text.setText(mTitles[position]);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.text.setText(mTitles[position]);return convertView;}private static class ViewHolder {TextView text;}}

這個適配器用處表較大,因為在MenuActivity中,會通過他擷取布局,這個地方我改了一下,為了實現重用我將布局數組該成了單個int 布局,

// final TypedArray layoutsArray = res// .obtainTypedArray(R.array.effect_layouts);// 這是幾個畫面的布局// final int count = layoutsArray.length();/* * mLayouts = new int[count]; for (int i = 0; i < count; ++i) { * mLayouts[i] = layoutsArray.getResourceId(i, 0); } * layoutsArray.recycle();// TypedArray這個用了之後一定要注意回收 */}
那我需要給每個Fragment一個action_layout 一個content_layout,否則會報錯。

package com.delvik.myprojectqiu;import java.util.ArrayList;import java.util.HashMap;import java.util.Map;import shared.ui.actionscontentview.ActionsContentView;import android.annotation.SuppressLint;import android.app.Fragment;import android.app.FragmentManager;import android.app.FragmentTransaction;import android.content.Context;import android.net.Uri;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.text.TextUtils;import android.view.LayoutInflater;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.TextView;import com.delvik.adapter.MenuAdapter;/** * @author delvikCoder *  *         使用的一些說明: *  *         layout必須要三個元素:actioncontentview *         ---->{action_layout(一樣的),content_layout(不同頁面不同layout)} *  *  */public class MenuActivity extends FragmentActivity {private static final String STATE_POSITION = "state:layout_id";private static final String SCHEME = "settings";private static final String AUTHORITY = "effects";public static final Uri URI = new Uri.Builder().scheme(SCHEME).authority(AUTHORITY).build();private MenuAdapter mAdapter;private ListView viewList;private LayoutInflater inflater;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mAdapter = new MenuAdapter(this);inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);final int selectedPosition;if (savedInstanceState != null) {selectedPosition = savedInstanceState.getInt(STATE_POSITION, 0);} else {selectedPosition = 0;}fragmentManager = getFragmentManager();initView(selectedPosition);}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);final int position = viewList.getSelectedItemPosition();if (position != ListView.INVALID_POSITION)outState.putInt(STATE_POSITION, position);}private FragmentManager fragmentManager;private FragmentTransaction fragmentTransaction;/** 當前顯示的fragment 標誌 **/private String currentFragmentTag;/** * <p> * 切換view 這裡就是直接New Fragment然後將其替換main_content中的content *  * </p> */public void initFragmentView(int position) {fragmentTransaction = checkTransaction();String tag = "";switch (position) {case 0:tag = mAdapter.getEffectTitle(position);switchView(String.valueOf(position));break;case 1:tag = mAdapter.getEffectTitle(position);switchView(String.valueOf(position));break;case 2:tag = mAdapter.getEffectTitle(position);switchView(String.valueOf(position));break;case 3:break;default:break;}}/** * <p> * 根據tag切換fragment * </p> */private void switchView(String tag) {// 點擊的同一個,就不處理if (TextUtils.equals(currentFragmentTag, tag)) {return;}if (currentFragmentTag != null) {detachFragment(getFragment(tag));}// R.id.content 我搞錯了,於是總是報// no view for id /////異常FKattachFragment(R.id.content, getFragment(tag), tag);currentFragmentTag = tag;commitTransactions();}/** * <p> * </p> *  * @param tag *            void */private Fragment getFragment(String tag) {Fragment fg = fragmentManager.findFragmentByTag(tag);if (fg == null) {Integer i = Integer.valueOf(tag);switch (i) {case 0:fg = new FirstFragment();break;case 1:fg = new SecondFragment();break;case 2:fg = new ThirdFragment();break;case 3:}}return fg;}/** * <p> * 隱藏Fragment * </p> */private void detachFragment(Fragment fg) {if (fg != null && !fg.isDetached()) {checkTransaction();fragmentTransaction.detach(fg);}}/** * <p> * 顯示或者添加fragment * </p> */private void attachFragment(int layout, Fragment f, String tag) {if (f != null) {if (f.isDetached()) {// 如果當前fragment只是隱藏,則顯示出來checkTransaction();fragmentTransaction.attach(f);} else if (!f.isAdded()) {// 如果當前fragment沒有添加到容器中,則先要添加到容器中checkTransaction();fragmentTransaction.add(layout, f, tag);}}}/** * <p> * * </p> */public FragmentTransaction checkTransaction() {if (fragmentTransaction == null) {fragmentTransaction = fragmentManager.beginTransaction();fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);}return fragmentTransaction;}/** * 儲存fragment狀態,添加或隱藏fragment最後都需要調用此函數 */protected void commitTransactions() {if (fragmentTransaction != null && !fragmentTransaction.isEmpty()) {fragmentTransaction.commit();fragmentTransaction = null;}}@SuppressLint("DefaultLocale")private void initView(int position) {// 擷取布局的idint layoutId = mAdapter.getItem(position);// int layoutId = mAdapter.getItem(position);setContentView(layoutId);initFragmentView(position);// 這裡是設定標題final String titleText = "action_effects";final TextView title = (TextView) findViewById(android.R.id.text1);title.setText(titleText.toUpperCase());final ActionsContentView viewActionsContentView = (ActionsContentView) findViewById(R.id.actionsContentView);viewActionsContentView.setOnActionsContentListener(new ActionsContentView.OnActionsContentListener() {@Overridepublic void onContentStateChanged(ActionsContentView v,boolean isContentShown) {v.getContentController().setIgnoreTouchEvents(!isContentShown);}@Overridepublic void onContentStateInAction(ActionsContentView v,boolean isContentShowing) {// TODO Auto-generated method stub}});viewList = (ListView) findViewById(R.id.actions);viewList.setAdapter(mAdapter);viewList.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> adapter, View v,int position, long flags) {initView(position);}});}public View getLayoutView(int layoutId) {View v = inflater.inflate(layoutId, null);return v;}}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.