Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android控件Gridview實現仿支付寶主頁,Fragment底部按鈕切換和登錄圓形頭像

Android控件Gridview實現仿支付寶主頁,Fragment底部按鈕切換和登錄圓形頭像

編輯:Android開發教程

Android控件Gridview實現仿支付寶首頁,Fragment底部按鈕切換和登錄圓形頭像

此案例主要講的是Android控件Gridview(九宮格)完美實現仿支付寶首頁,包含添加和刪除功能;Fragment底部按鈕切換的效果,包含四個模塊,登錄頁面圓形頭像等,一個小項目的初始布局。

效果圖:

           

自定義GridView

 

<menu.menudemo.ui.view.ZQScrollGridView
    android:id="@+id/home_gridview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#E4E4E4"
    android:cacheColorHint="#00000000"
    android:fadingEdge="none"
    android:horizontalSpacing="1dp"
    android:listSelector="#00000000"
    android:numColumns="3"
    android:scrollbars="none"
    android:verticalSpacing="1dp"
    ></menu.menudemo.ui.view.ZQScrollGridView>
/**
 * 不能滾動的GridView,用於嵌套在如ScrollView之類的可滾動的控件中
 */
public class ZQScrollGridView extends GridView {
	  
	public ZQScrollGridView(Context context){
        this(context, null);  
   } 
	
	public ZQScrollGridView(Context context, AttributeSet attrs){
         super(context, attrs);  
    }  
 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){  
         int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
         super.onMeasure(widthMeasureSpec, mExpandSpec);  
    } 
} 


首頁代碼

 

/**
 * 首頁
 */
public class HomeFragment extends Fragment implements  View.OnClickListener {

    private View view;
    private TextView tv_sign;
    private Intent intent;
    private ZQScrollGridView gridView;

    private static HomeAdapter adapter;
    public static List<Integer> listPosition;
    public static List<UserBean> list;
    private static ACache aCache;

    private Context context;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        view = getView();
        aCache = ACache.get(getActivity());
        context = getActivity();
        initView();
    }

    public void initView() {
        tv_sign = (TextView) view.findViewById(R.id.home_sign);
        tv_sign.setOnClickListener(this);
        gridView = (ZQScrollGridView) view.findViewById(R.id.home_gridview);
        onLoad();
    }


    public void onLoad() {
        adapter = new HomeAdapter(getActivity());
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(onItemClickListener);
        gridView.setOnItemLongClickListener(onItemLongClickListener);
        showData();
    }

    /*****
     * 刷新數據
     */
    private void notifyData() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

    public Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                showLongClick(false);
            } else if (msg.what == 2)//刪除
            {
                showDelete();
            }
        }
    };


    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (position == (listPosition.size() - 1)) {
                intent = new Intent(getActivity(), SearchActivity.class);
                intent.putExtra("list", (Serializable) listPosition);
                startActivityForResult(intent, 1);
            }
            else {

            }

        }
    };
    AdapterView.OnItemLongClickListener onItemLongClickListener = new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

            if (position == (listPosition.size() - 1)) {
                return false;
            }
            showLongClick(true);
            Message msg = new Message();
            msg.what = 1;// 刪除
            msg.obj = 1;
            LoginFragmentActivity.handler.sendMessage(msg);
            return false;
        }
    };



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == 1) {
                if (data.getStringExtra("key").equals("2")) {
                    listPosition.clear();
                    listPosition = HomeData.POSITION(aCache);
                    notifyData();
                }
            }
        }
    }

    private void showData() {
        listPosition = HomeData.POSITION(aCache);
        list = new ArrayList<UserBean>();
        for (int i = 0; i < 34; i++) {
            UserBean user = new UserBean(HomeData.IMG[i], HomeData.TITLE[i], false, false);
            list.add(user);
        }
        notifyData();
    }

    /***
     * 刪除
     */
    private void showDelete() {
        //刪除緩存
        String home = "";
        for (int i = 0; i < listPosition.size() - 1; i++) {
            UserBean userBean = list.get(listPosition.get(i));
            if (!userBean.isCheck()) {
                home += listPosition.get(i) + ",";
            }
        }
        aCache.remove("home");
        listPosition.clear();
        try {
            UtilFileDB.ADDFile(aCache, "home", home.substring(0, (home.length() - 1)));
            if (listPosition == null || listPosition.size() <= 1) {
                listPosition = HomeData.POSITION(aCache);
            }
        } catch (Exception e) {
            listPosition.add((list.size()-1));//只留加號
        }
        showLongClick(false);
    }

    /****
     * 重新刷新數據
     *
     * @param isvisibility
     */
    private void showLongClick(boolean isvisibility) {
        list.clear();
        for (int i = 0; i < 34; i++) {
            UserBean user = new UserBean(HomeData.IMG[i], HomeData.TITLE[i], false, isvisibility);
            list.add(user);
        }
        HomeFragment.list.get(33).setIsvisibility(false);
        notifyData();
    }
}


由於代碼太多未完全給出,源碼直接下載即可(點擊下載)

 

源碼點擊下載

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved