私は自分のアプリケーションでviewpagerを使用したいと思います。私は毎月これを1ヶ月でやってみましたが、私は解決策を達成できません。同じリストビューの概念ではなく異なるデータを作成します。ここに私のコードは次のとおりです。私のアプリケーションでビューページャーを使用する
public final class TestFragment extends ListFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
ArrayList <HashMap<String, Object>> imageliste = new ArrayList<HashMap<String, Object>>();
public class MyCustomAdapter extends ArrayAdapter<HashMap<String, Object>> {
//Bitmap bm;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String,Object>> imageliste) {
super(context, textViewResourceId,imageliste);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if(row==null){
LayoutInflater inflater=LayoutInflater.from(getActivity());
row=inflater.inflate(R.layout.list, parent, false);
}
TextView label=(TextView)row.findViewById(R.id.text1);
label.setText((CharSequence) imageliste.get(position).get("Baslik"));
TextView label2=(TextView)row.findViewById(R.id.text2);
int boyut =imageliste.get(position).get("Desc").toString().length();
label2.setText((CharSequence) imageliste.get(position).get("Desc").toString().substring(0, (boyut/3)*2)+"...");
ImageView icon=(ImageView)row.findViewById(R.id.img);
icon.setImageDrawable((Drawable) imageliste.get(position).get("Resim"));
return row;
}
}
public String getURLContent(String url)
{
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
return page;
} catch (ClientProtocolException e) {
return "";
} catch (IOException e) {
return "";
}
}
public ArrayList<HashMap<String, Object>> getImageLinks(String strng){
ArrayList<HashMap<String, Object>> myBooks2 = new ArrayList<HashMap<String, Object>>();
String html = getURLContent(strng);
Document doc = Jsoup.parse(html);
Elements divs = doc.getElementsByClass("postBox");
for (Element div : divs) {
Element masthead = div.select("img[src].attachment-post-thumbnail").first();
String linkHref = masthead.attr("src");
Element masthead2 = div.select("h1").first().select("a").first();
String baslik = masthead2.text();
Element masthead3 = div.select("div.textPreview").first().select("p").first();
String desc = masthead3.text();
//Drawable drawable = LoadImageFromWebOperations();
HashMap<String, Object> hm = new HashMap<String, Object>();
hm.put("Resim", LoadImageFromWebOperations(linkHref));
hm.put("Baslik", baslik);
hm.put("Desc", desc);
myBooks2.add(hm);
}
return myBooks2;
}
private Drawable LoadImageFromWebOperations(String url){
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
public class backgroundLoadListView extends AsyncTask<String, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(getActivity());
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
// adapter = new MyCustomAdapter(getActivity().getApplicationContext(), R.layout.list, imageliste);
//adapter.notifyDataSetChanged();
dialog.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog.setMessage("Yükleniyor...");
dialog.show();
}
@Override
protected Void doInBackground(String... arg) {
// TODO Auto-generated method stub
imageliste=getImageLinks(arg[0]);
return null;
}
}
public class backgroundLoadListView2 extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.customslidingtabhost, null);
ListView listView1=(ListView)view.findViewById(R.id.list);
//MyCustomAdapter adapter = new MyCustomAdapter(getActivity(), R.layout.list, imageliste);
//listView1.setAdapter(adapter);
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
listView1.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
listView1.setDividerHeight(2);
listView1.setBackgroundColor(Color.WHITE);
((PullToRefreshListView) listView1).onRefreshComplete();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
imageliste=getImageLinks("http://www.teknoinfo.net/kategoriler/haberler/teknoloji-haberleri");
return null;
}
}
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
return fragment;
}
private String mContent = "???";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
//new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
MyCustomAdapter adapter = new MyCustomAdapter(getActivity().getApplicationContext(), R.layout.list, imageliste);
View view = inflater.inflate(R.layout.customslidingtabhost, null);
final ListView v=(ListView)view.findViewById(R.id.list);
/*((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
new backgroundLoadListView2().execute();
}
});*/
int[] colors = {0xFFFFFFFF, 0xFF87CEEB, 0xFFFFFFFF}; // red for the example
v.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
v.setDividerHeight(2);
v.setBackgroundColor(Color.WHITE);
v.setAdapter(adapter);
((PullToRefreshListView) v).setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
//new backgroundLoadListView2().execute();
// new backgroundLoadListView().execute("http://www.teknoinfo.net/haberler");
}
});
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}