多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 項目實戰①―高仿知乎日報(2)―>使用pullrefesh+Slidingmenu+自定義組件寫主布局2

項目實戰①―高仿知乎日報(2)―>使用pullrefesh+Slidingmenu+自定義組件寫主布局2

來源:程序員人生   發布時間:2014-12-14 08:36:51 閱讀次數:3830次

在這1篇開頭我要感謝我的老師李衛民同志,沒有他這個東西做不出來,有了他這個Demo逼近真實的XX日報


現在清晨了,似乎大腦更加清晰了,有助于我清算下思路,繼續寫完我的博客,上1節講完了,Slidingmenu的內容填充,既然將內容填充好了,似乎要加點樂子,那就是listview的點擊事件。

① listview的點擊事件處理

本來以為首頁的數據和 其他頁面的數據是1樣的也就是我想用1個Fragment將所有的數據都寫在里面 ,看來是不可能的了。我先不寫主布局的Fragment 由于里面藏著1個自定義的1個類,先寫其他頁面的數據可以從易到難漸漸來
/* -------------------- ListView點擊事件 -------------------- */ @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 關閉側滑菜單 menu.toggle(); if (position == 0) { // 進入首頁 initView(); } else { // 填充Fragment ThemeOther other = theme.getOthers().get(position); cTitle.setTitle(other.getName()); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.replace(R.id.fl_content, ContentFragment.newInstance(other.getId(), other.getDescription(), other.getImage())); fragmentTransaction.commit(); } }

其實只是1個主布局的Framlayout在變 所以我們可以先將 標題給變了,然后再將需要傳遞的ID (拼接URL)description (Fragment)的標題  image Fragment最上面的圖片 這么說有點抽象 看看布局吧




是否是1目了然,和我說的1樣,主要了 在Fragment里面 傳值不要用構造方法 的bundle 這樣會使Fragment 很多東西不能實現   要改1種方式 newInstance

②Fragment的代碼實現

對Fragment 最1開始感覺挺不適應他的,由于總是和Viewpage1起出現,然后本來1個1個出現比較簡單,1起出現我就懵了,后面越用用方便,最后發現其實 activity用的愈來愈少了......................它的生命周期很重要......

1取得從activity傳來的數據

public static ContentFragment newInstance(long id, String description, String image) { ContentFragment f = new ContentFragment(); Bundle args = new Bundle(); args.putLong("id", id); args.putString("description", description); args.putString("image", image); f.setArguments(args); return f; }

2 在 Oncreate 里面將數據準備好

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); id = getArguments().getLong("id"); description = getArguments().getString("description"); image = getArguments().getString("image"); mQueue = Volley.newRequestQueue(getActivity()); }

2 在OncreateView里面初始化界面

固然首先要在里面填充1個布局羅,其實你看下Activity的源碼 其實setContentView是和Fragment的 inflate 是相差不大的

View view = inflater.inflate(R.layout.fragment_content, container, false);
我給大家看看布局吧 免得下面的界面命名看不懂 
<?xml version="1.0" encoding="utf⑻"?> <LinearLayout 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" android:orientation="vertical" > <ScrollView android:id="@+id/sv_content" android:layout_width="match_parent" android:layout_height="match_parent" tools:ignore="UselessLeaf,UselessParent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp" tools:ignore="UselessLeaf,UselessParent" > <com.android.volley.toolbox.NetworkImageView android:id="@+id/img_content" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" tools:ignore="ContentDescription" /> <TextView android:id="@+id/tx_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="10dp" android:padding="10dp" android:textColor="#FFFFFF" android:textSize="18sp" /> </RelativeLayout> <com.qf.teach.project.zhihudaily.custom.CustomListViewForScrollView android:id="@+id/lv_theme" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="#EEEEEE" android:dividerHeight="5dp" android:padding="10dp" /> </LinearLayout> </ScrollView> </LinearLayout>

里面有1個自定的listview 目的是解決和ScrollView搶焦點的問題
package com.qf.teach.project.zhihudaily.custom; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; public class CustomListViewForScrollView extends ListView { public CustomListViewForScrollView(Context context) { super(context); } public CustomListViewForScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomListViewForScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }

回到java 代碼....................
/**初始化界面 * @param view */ private void initView(View view) { //設置廣告頁標題 txDesc = (TextView) view.findViewById(R.id.tx_desc); txDesc.setText(description); //設置廣告頁的圖片 imgContent = (NetworkImageView) view.findViewById(R.id.img_content); imgContent.setImageUrl(image, new ImageLoader(mQueue, new BitmapCache())); adapter = new MyBaseAdapter(); //自定義listview lvTheme = (CustomListViewForScrollView) view.findViewById(R.id.lv_theme); lvTheme.setOnItemClickListener(this); lvTheme.setAdapter(adapter); svContent = (ScrollView) view.findViewById(R.id.sv_content); }

3初始化數據―利用volly聯網得到json

/** * 初始化數據 */ private void initData() { mQueue.add(new JsonObjectRequest(Method.GET, String.format(API.getTheme(), id), null, this, null)); }

我們先看看json結構吧 相信大家json解析都會 我就不敘述了


其中的Stringformat 我在前面已說過 利用占位符 將后面的參數ID傳進,拼接字符串

/* -------------------- 網絡要求 -------------------- */ @Override public void onResponse(JSONObject response) { try { themeStory = new ThemeStory(); themeStory.setDescription(response.getString("description")); themeStory.setBackground(response.getString("background")); themeStory.setImage(response.getString("image")); themeStory.setColor(response.getInt("color")); themeStory.setImage_source(response.getString("image_source")); themeStory.setName(response.getString("name")); // 解析stories JSONArray arrayStories = response.getJSONArray("stories"); if (arrayStories != null && arrayStories.length() > 0) { List<Story> stories = new ArrayList<Story>(); for (int i = 0 ; i < arrayStories.length() ; i++) { JSONObject obj = arrayStories.getJSONObject(i); Story story = new Story(); story.setType(obj.getInt("type")); story.setId(obj.getLong("id")); story.setShare_url(obj.getString("share_url")); story.setTitle(obj.getString("title")); if (obj.has("multipic")) { story.setMultipic(obj.getBoolean("multipic")); } // 圖片數組 if (obj.has("images")) { JSONArray array = obj.getJSONArray("images"); if (array != null && array.length() > 0) { String[] images = new String[array.length()]; for (int x = 0 ; x < array.length() ; x++) { images[x] = array.getString(x); } story.setImages(images); } } stories.add(story); } themeStory.setStories(stories); } // 解析editors JSONArray arrayEditors = response.getJSONArray("editors"); if (arrayEditors != null && arrayEditors.length() > 0) { List<Editor> editors = new ArrayList<Editor>(); for (int i = 0 ; i < arrayEditors.length() ; i++) { JSONObject obj = arrayEditors.getJSONObject(i); Editor editor = new Editor(); editor.setAvatar(obj.getString("avatar")); editor.setId(obj.getLong("id")); editor.setName(obj.getString("name")); editors.add(editor); } themeStory.setEditors(editors); } // 由于斟酌到 URl會變所以我們通知數據產生改變 adapter.notifyDataSetChanged(); //解決1開始進來不在最上面的問題 svContent.smoothScrollTo(0, 0); } catch (JSONException e) { e.printStackTrace(); } }



4 填充adapter里面的數據

沒啥好說的 看代碼吧
/* -------------------- 網絡要求 -------------------- */ class MyBaseAdapter extends BaseAdapter { private ViewHolder viewHolder; @Override public int getCount() { return themeStory == null ? 0 : themeStory.getStories().size(); } @Override public Object getItem(int position) { return themeStory.getStories().get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_theme_news, parent, false); viewHolder = new ViewHolder(); viewHolder.txTitle = (TextView) convertView.findViewById(R.id.tx_title); viewHolder.imgThumb = (NetworkImageView) convertView.findViewById(R.id.img_thumb); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Story story = themeStory.getStories().get(position); viewHolder.txTitle.setText(story.getTitle()); viewHolder.imgThumb.setVisibility(View.GONE); if (story.getImages() != null && story.getImages().length > 0) { viewHolder.imgThumb.setImageUrl(story.getImages()[0], new ImageLoader(mQueue, new BitmapCache())); viewHolder.imgThumb.setVisibility(View.VISIBLE); } return convertView; } class ViewHolder { public TextView txTitle; public NetworkImageView imgThumb; } }






生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产高清精品一级毛片 | 国产一区二区免费不卡在线播放 | 久久久久亚洲精品影视 | 午夜视频在线观看免费视频 | 黄色福利网 | 538在线视频二三区视视频 | 欧美日韩一区二区三区免费 | 国产福利在线观看永久免费 | 亚洲欧美一区二区三区综合 | 国久久 | 在线观看视频免费入口 | 久久99亚洲精品一区二区 | 国产免费叼嘿网站免费 | 亚洲色欲色欲综合网站 | 日本免费一区二区视频 | h视频在线观看免费 | 欧美熟妇下面毛毛黑森林 | xxxx欧美| 一级一毛片a级毛片欧美 | 一本大道香蕉久在线不卡视频 | 欧美亚洲国产片在线观看 | 中文字幕在线观看免费视频 | 最新国产精品精品视频 | 国产精品久久久久久久毛片 | 久久久久欧美国产精品 | 亚洲综合网址 | 日本无卡无吗中文免费 | 免费乱码中文字幕网站 | 波多野一区二区三区在线 | 欧美性猛交xxxx乱大交 | 国产xxx护士爽免费看 | 欧美另类xxxx | 亚洲黄色片免费看 | 欧美亚洲精品在线 | 国产午夜精品不卡视频 | 美国一级毛片在线 | 一本久道久久综合中文字幕 | 一级女性全黄生活片免费看 | 亚洲欧美日韩中文字幕在线一区 | 免费亚洲视频在线观看 | 91久久精品国产亚洲 |