-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
使用DirectionViewPager代替viewflipper,并且进行一次代码重构,目前没有bug,没有运行
- Loading branch information
1 parent
26c58a6
commit 718b145
Showing
11 changed files
with
582 additions
and
506 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.adapter; | ||
|
||
import com.example.fragment.MainHotStoriesFragment; | ||
|
||
import android.support.v4.app.Fragment; | ||
import android.support.v4.app.FragmentManager; | ||
import android.support.v4.app.FragmentPagerAdapter; | ||
|
||
public class HotStoriesPagersAdapter extends FragmentPagerAdapter { | ||
|
||
private final String[] TITLES = { "one", "two", "three", "four", "five"}; | ||
|
||
public HotStoriesPagersAdapter(FragmentManager fm) { | ||
super(fm); | ||
} | ||
|
||
@Override | ||
public CharSequence getPageTitle(int position) { | ||
return TITLES[position]; | ||
} | ||
|
||
@Override | ||
public int getCount() { | ||
return TITLES.length; | ||
} | ||
|
||
@Override | ||
public Fragment getItem(int position) { | ||
return MainHotStoriesFragment.newInstance(position); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.example.adapter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.BaseAdapter; | ||
import android.widget.ImageView; | ||
import android.widget.TextView; | ||
|
||
import com.example.zhihupocket.R; | ||
|
||
public class StoriesAdapter extends BaseAdapter{ | ||
|
||
private LayoutInflater mInflater = null; | ||
private ArrayList<HashMap<String, Object>> stories_group; | ||
//构造函数 | ||
public StoriesAdapter(Context context, ArrayList<HashMap<String, Object>> stories_group) { | ||
// TODO Auto-generated constructor stub | ||
this.mInflater = LayoutInflater.from(context); | ||
this.stories_group = stories_group; | ||
} | ||
|
||
|
||
@Override | ||
public int getCount(){ | ||
return stories_group.size(); | ||
} | ||
|
||
@Override | ||
public Object getItem(int arg0){ | ||
return null; | ||
} | ||
|
||
@Override | ||
public long getItemId(int arg0){ | ||
return 0; | ||
} | ||
|
||
@Override | ||
public View getView(int position, View convertView, ViewGroup Parent){ | ||
PicAndTextViewGroup picandtext = null; | ||
if(convertView == null){ | ||
picandtext = new PicAndTextViewGroup(); | ||
convertView = mInflater.inflate(R.layout.main_lv_item, null); | ||
picandtext.iv_story_img = (ImageView)convertView.findViewById(R.id.iv_story_img); | ||
picandtext.tv_story_title = (TextView)convertView.findViewById(R.id.tv_story_title); | ||
convertView.setTag(picandtext); | ||
} | ||
else{ | ||
picandtext = (PicAndTextViewGroup)convertView.getTag(); | ||
} | ||
|
||
// 有图片的话显示图片 | ||
if (stories_group.get(position).containsKey("imguri")) { | ||
picandtext.iv_story_img.setImageURI((Uri)stories_group.get(position).get("imguri")); | ||
} | ||
picandtext.tv_story_title.setText(stories_group.get(position).get("title").toString()); | ||
|
||
return convertView; | ||
} | ||
|
||
private class PicAndTextViewGroup{ | ||
ImageView iv_story_img; | ||
TextView tv_story_title; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.example.fragment; | ||
|
||
import com.example.zhihupocket.R; | ||
|
||
import android.os.Bundle; | ||
import android.provider.ContactsContract.CommonDataKinds.Im; | ||
import android.support.v4.app.Fragment; | ||
import android.util.TypedValue; | ||
import android.view.LayoutInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.FrameLayout; | ||
import android.widget.ImageView; | ||
import android.widget.FrameLayout.LayoutParams; | ||
import android.widget.TextView; | ||
|
||
public class MainHotStoriesFragment extends Fragment { | ||
|
||
private static final String ARG_POSITION = "position"; | ||
|
||
private int position; | ||
|
||
public static MainHotStoriesFragment newInstance(int position) { | ||
MainHotStoriesFragment f = new MainHotStoriesFragment(); | ||
Bundle b = new Bundle(); | ||
b.putInt(ARG_POSITION, position); | ||
f.setArguments(b); | ||
return f; | ||
} | ||
|
||
@Override | ||
public void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
|
||
position = getArguments().getInt(ARG_POSITION); | ||
} | ||
|
||
@Override | ||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
|
||
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); | ||
|
||
FrameLayout fl = (FrameLayout)getResources().getLayout(R.id.rl_contain_topstories); | ||
ImageView pic = (ImageView)fl.getChildAt(0); | ||
TextView txt = (TextView)fl.getChildAt(1); | ||
|
||
return fl; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.listener; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.view.View; | ||
import android.widget.AdapterView; | ||
import android.widget.AdapterView.OnItemClickListener; | ||
|
||
import com.example.zhihupocket.StoryContent; | ||
|
||
public class StoryItemClickListener implements OnItemClickListener{ | ||
Context context; | ||
ArrayList<HashMap<String, Object>> stories_group; | ||
public StoryItemClickListener(Context context, ArrayList<HashMap<String, Object>> stories_group) { | ||
// TODO Auto-generated constructor stub | ||
this.context = context; | ||
this.stories_group = stories_group; | ||
} | ||
@Override | ||
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, | ||
long arg3) { | ||
// TODO Auto-generated method stub | ||
// 将stories和顺序传递过去 | ||
Intent intent = new Intent(context, StoryContent.class); | ||
intent.putExtra("stories_group", stories_group); | ||
intent.putExtra("story_order", arg2); | ||
context.startActivity(intent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.example.task; | ||
|
||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpStatus; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.impl.client.DefaultHttpClient; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import com.example.zhihupocket.MainActivity; | ||
|
||
public class HttpRequestData { | ||
|
||
// 通过请求URL获取数据,返回-1的话代表连接失败 | ||
public static String getJsonContent(){ | ||
HttpClient httpclient = null; | ||
try{ | ||
//httpclient对象 | ||
httpclient = new DefaultHttpClient(); | ||
//httpget对象,构造 | ||
HttpGet httpget = new HttpGet(MainActivity.ZHIHU_API); | ||
//请求httpclient获得httpresponse | ||
HttpResponse httpresponse = httpclient.execute(httpget); | ||
if(httpresponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ | ||
|
||
HttpEntity httpEntity = httpresponse.getEntity(); | ||
//获得返回的字符串 | ||
String json_data = EntityUtils.toString(httpEntity, "utf-8"); | ||
return json_data; | ||
} | ||
else{ | ||
//中断连接 | ||
httpget.abort(); | ||
} | ||
} | ||
catch(Exception e){ | ||
e.printStackTrace(); | ||
} | ||
httpclient.getConnectionManager().shutdown(); | ||
return "-1"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package com.example.task; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import android.net.Uri; | ||
|
||
import com.example.zhihupocket.MainActivity; | ||
|
||
public class ParseJsonHotStories { | ||
|
||
ArrayList<HashMap<String, Object>> topstories_group = new ArrayList<HashMap<String,Object>>(); | ||
public ParseJsonHotStories(String json_data){ | ||
transJsonTopStoriesIntoArrayList(json_data); | ||
} | ||
|
||
public ArrayList<HashMap<String, Object>> getHotStories(){ | ||
return topstories_group; | ||
} | ||
|
||
// 将json格式的数据转化成arraylist里的数据 | ||
public void transJsonTopStoriesIntoArrayList(String json_data){ | ||
HashMap<String, Object> story_item; | ||
try { | ||
JSONObject json_parse_object = new JSONObject(json_data); | ||
JSONArray json_topstories = json_parse_object.getJSONArray("top_stories"); | ||
//循环添加数据 | ||
for(int i=0;i<json_topstories.length();i++){ | ||
story_item = new HashMap<String, Object>(); | ||
story_item.put("title", json_topstories.getJSONObject(i).getString("title")); | ||
// 最近知乎改了接口,原来的接口不能用了 | ||
if(json_topstories.getJSONObject(i).has("share_url")){ | ||
story_item.put("share_url", json_topstories.getJSONObject(i).getString("share_url")); | ||
} | ||
else { | ||
story_item.put("share_url", MainActivity.ZHIHU_STORY_API+json_topstories.getJSONObject(i).getString("id")); | ||
} | ||
String str = json_topstories.getJSONObject(i).getString("image"); | ||
str = getHandledURL(str); | ||
|
||
story_item.put("image", str); | ||
// 同时异步获取图片的uri | ||
story_item.put("imguri", downloadPic(str, MainActivity.pic_cache)); | ||
topstories_group.add(story_item); | ||
} | ||
} catch (Exception e) { | ||
// TODO: handle exception | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// 从网络上下载图片 | ||
public Uri downloadPic(String path, File cache){ | ||
String name = getPicNameOfUrl(path); | ||
File file = new File(cache, name); | ||
if(file.exists()){ | ||
//这个方法能够获得文件的URI | ||
return Uri.fromFile(file); | ||
}else { | ||
//从网络上获取图片 | ||
try{ | ||
URL url = new URL(path); | ||
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); | ||
conn.setConnectTimeout(5000); | ||
conn.setRequestMethod("GET"); | ||
conn.setDoInput(true); | ||
if (conn.getResponseCode() == 200) { | ||
InputStream is = conn.getInputStream(); | ||
FileOutputStream fos = new FileOutputStream(file); | ||
byte[] buffer = new byte[1024]; | ||
int len = 0; | ||
while ((len = is.read(buffer)) != -1) { | ||
fos.write(buffer, 0, len); | ||
} | ||
is.close(); | ||
fos.close(); | ||
// 返回一个URI对象 | ||
return Uri.fromFile(file); | ||
} | ||
}catch(Exception e){ | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
public String getHandledURL(String ori_url){ | ||
String step_one = ori_url.replace("\\", ""); | ||
String step_two = step_one.replace("[", ""); | ||
String step_three = step_two.replace("]", ""); | ||
String step_four = step_three.replace("\"", ""); | ||
return step_four; | ||
} | ||
|
||
// 获得图片的名称 | ||
public String getPicNameOfUrl(String name){ | ||
String[] item_group = name.split("/"); | ||
return item_group[item_group.length-1]; | ||
} | ||
} |
Oops, something went wrong.