編輯:Android技術基礎
相信大家對點擊列表,然後進入詳情這種App並不陌生吧,在購物類App和新聞類App中最為常見: 下面我們簡單來講一下流程邏輯!
剛好公司測試妹子的測試機上裝了楚楚街9塊9的APP,呵呵,直接就照這個來研究吧:
嘿嘿,市面上很多APP都是這種樣子的,而這個可以用我們學到的Fragment來實現: 可能gif動畫看不清,筆者用界面原型工具畫個大概吧:
大概就這樣,中間區域是一個布局容器,一般是FrameLayout,然後我們將一個Fragment replace 到這個容器中或者add也行,而這個Fragment中有一個listview,當我們點擊這個ListView中的一項, 中間容器中的Fragment就會被replace成對應詳細信息的Fragment所替代,如果我們只是replace的話, 就不會保存第一個Fragment的狀態,用戶又得從頭開始浏覽,這肯定是很不方便的,這裡我們可以 通過Fragment棧的addtobackStack和popbackstack來解決這個問題!當replace的同時,我們將被替換 的Fragment添加到stack中,當用戶點擊回退按鈕時,調用popbackstack彈出棧,具體實現見下述代碼 示例!
運行效果圖:
實現代碼:
Step 1:先把兩個Fragment以及Activity的布局實現了
fg_newlist.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:background="@color/white" android:orientation="horizontal"> <ListView android:id="@+id/list_news" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
fg_context.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"> <TextView android:id="@+id/txt_content" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textColor="@color/blue" android:textSize="20sp" /> </LinearLayout>
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/txt_title" android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/blue" android:textColor="@color/white" android:text="新聞列表" android:textSize="20sp" android:text android:gravity="center"/> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/txt_title"/> </RelativeLayout>
Step 2:實現我們的業務Bean類和自定義BaseAdapter類:
Data.java:
/** * Created by Jay on 2015/9/6 0006. */ public class Data { private String new_title; private String new_content; public Data(){} public Data(String new_title, String new_content) { this.new_title = new_title; this.new_content = new_content; } public String getNew_title() { return new_title; } public String getNew_content() { return new_content; } public void setNew_title(String new_title) { this.new_title = new_title; } public void setNew_content(String new_content) { this.new_content = new_content; } }
MyAdapter.java:
/** * Created by Jay on 2015/9/6 0006. */ public class MyAdapter extends BaseAdapter{ private List<Data> mData; private Context mContext; public MyAdapter(List<Data> mData, Context mContext) { this.mData = mData; this.mContext = mContext; } @Override public int getCount() { return mData.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if(convertView == null){ convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false); viewHolder = new ViewHolder(); viewHolder.txt_item_title = (TextView) convertView.findViewById(R.id.txt_item_title); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.txt_item_title.setText(mData.get(position).getNew_title()); return convertView; } private class ViewHolder{ TextView txt_item_title; } }
Step 3:MainActivity的實現
MainActivity.java:
public class MainActivity extends AppCompatActivity { private TextView txt_title; private FrameLayout fl_content; private Context mContext; private ArrayList<Data> datas = null; private FragmentManager fManager = null; private long exitTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; fManager = getFragmentManager(); bindViews(); datas = new ArrayList<Data>(); for (int i = 1; i <= 20; i++) { Data data = new Data("新聞標題" + i, i + "~新聞內容~~~~~~~~"); datas.add(data); } NewListFragment nlFragment = new NewListFragment(fManager, datas); FragmentTransaction ft = fManager.beginTransaction(); ft.replace(R.id.fl_content, nlFragment); ft.commit(); } private void bindViews() { txt_title = (TextView) findViewById(R.id.txt_title); fl_content = (FrameLayout) findViewById(R.id.fl_content); } //點擊回退鍵的處理:判斷Fragment棧中是否有Fragment //沒,雙擊退出程序,否則像是Toast提示 //有,popbackstack彈出棧 @Override public void onBackPressed() { if (fManager.getBackStackEntryCount() == 0) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { super.onBackPressed(); } } else { fManager.popBackStack(); txt_title.setText("新聞列表"); } } }
Step 4:列表Fragment的實現:
NewListFragment.java:
package com.jay.fragmentdemo4; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import java.util.ArrayList; /** * Created by Jay on 2015/9/6 0006. */ public class NewListFragment extends Fragment implements AdapterView.OnItemClickListener { private FragmentManager fManager; private ArrayList<Data> datas; private ListView list_news; public NewListFragment(FragmentManager fManager, ArrayList<Data> datas) { this.fManager = fManager; this.datas = datas; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_newlist, container, false); list_news = (ListView) view.findViewById(R.id.list_news); MyAdapter myAdapter = new MyAdapter(datas, getActivity()); list_news.setAdapter(myAdapter); list_news.setOnItemClickListener(this); return view; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { FragmentTransaction fTransaction = fManager.beginTransaction(); NewContentFragment ncFragment = new NewContentFragment(); Bundle bd = new Bundle(); bd.putString("content", datas.get(position).getNew_content()); ncFragment.setArguments(bd); //獲取Activity的控件 TextView txt_title = (TextView) getActivity().findViewById(R.id.txt_title); txt_title.setText(datas.get(position).getNew_content()); //加上Fragment替換動畫 fTransaction.setCustomAnimations(R.anim.fragment_slide_left_enter, R.anim.fragment_slide_left_exit); fTransaction.replace(R.id.fl_content, ncFragment); //調用addToBackStack將Fragment添加到棧中 fTransaction.addToBackStack(null); fTransaction.commit(); } }
Step 5:內容Fragment的實現:
NewContentFragment.java:
/** * Created by Jay on 2015/9/6 0006. */ public class NewContentFragment extends Fragment { NewContentFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fg_content, container, false); TextView txt_content = (TextView) view.findViewById(R.id.txt_content); //getArgument獲取傳遞過來的Bundle對象 txt_content.setText(getArguments().getString("content")); return view; } }
代碼很簡單,就不慢慢解釋了~
FragmentDemo5.zip:下載 FragmentDemo5.zip
因為時間的關系,並沒有詳細的去做過多的講解,示例代碼也很簡單,方便各位初學者理解! 如果要用到實際項目中還需要對此進行一番修改~!好的,本節就到這裡,謝謝~
Android手機上,我們常用ImageView顯示圖片,我們本章獲取網絡圖片並顯示在ImageView中。一、設計界面1、布局文件打開res/layout/activi
1.Android背景與當前的狀況Android系統是由Andy Rubin創建的,後來被Google收購了;最早的版本是:Android 1.1版
本節引言本節給大家介紹的是WebView下載文件的知識點,當我們在使用普通浏覽器的時候,比如UC,當我們點擊到一個可供下載鏈接的時候,就會進行下載,
我們在查航班信息時,或者在百度、Google搜索時,當你在搜索框裡輸入一些字符時,會自動彈出一個下拉框提示類似的結果。在Android中提供了兩種智能輸入框,它們是Aut