android – ViewPager指标与自定义ListView

android – ViewPager指标与自定义ListView,第1张

概述首先,我为我的英语道歉,因为它不是很好.谢谢. 我的应用程序包含很少的带适配器的ListWiews. ListView的数据来自位于Internet中的已解析 XML文件. 现在我有一个TabHost,每个选项卡中有三个选项卡和不同的ListView. 我想将TabHost更改为新的和轻微的方法–Jake Wharton的ViewPager指标(https://github.com/JakeWha @H_403_2@ 首先,我为我的英语道歉,因为它不是很好.谢谢. @H_301_7@我的应用程序包含很少的带适配器的ListWIEws. ListVIEw的数据来自位于Internet中的已解析 XML文件. @H_301_7@现在我有一个TabHost,每个选项卡中有三个选项卡和不同的ListVIEw. @H_301_7@我想将TabHost更改为新的和轻微的方法–Jake Wharton的VIEwPager指标(https://github.com/JakeWharton/ Android-VIEwPagerIndicator),因为有很棒的滑动效果.

我试图在示例项目中包含此代码,并且运行良好.@H_301_7@现在我在VIEwPager的一个片段中有一个ListVIEw,在另外两个片段中有一个简单的TextVIEw.它正在发挥作用.

VIEwPagerAdapter代码如下所示:

package com.formatbce.pager;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import androID.content.Context;import androID.os.Parcelable;import androID.support.v4.vIEw.PagerAdapter;import androID.support.v4.vIEw.VIEwPager;import androID.vIEw.VIEw;import androID.Widget.AbsListVIEw;import androID.Widget.AbsListVIEw.OnScrollListener;import androID.Widget.ListVIEw;import androID.Widget.SimpleAdapter;import androID.Widget.TextVIEw;import com.vIEwpagerindicator.TitleProvIDer;public class VIEwPagerAdapter extends PagerAdapterimplements TitleProvIDer{    private int[] scrollposition = new int[Titles.length];private static String[] Titles = new String[]{    "News","Bio","Music"};private final Context context;public VIEwPagerAdapter( Context context ){    this.context = context;    for ( int i = 0; i < Titles.length; i++ )    {        scrollposition[i] = 0;    }}@OverrIDepublic String getTitle( int position ){    return Titles[ position ];}@OverrIDepublic int getCount(){    return Titles.length;}@OverrIDepublic Object instantiateItem( VIEw pager,final int position ){    if (position == 0) {     ListVIEw v = new ListVIEw( context );        String[] from = new String[] { "str" };        int[] to = new int[] { androID.R.ID.text1 };        List<Map<String,String>> items =            new ArrayList<Map<String,String>>();        for ( int i = 0; i < 20; i++ )        {            Map<String,String> map =                new HashMap<String,String>();            map.put( "str",String.format( "Item %d",i + 1 ) );            items.add( map );        }        SimpleAdapter adapter = new SimpleAdapter( context,items,androID.R.layout.simple_List_item_1,from,to );        v.setAdapter( adapter );        ( (VIEwPager) pager ).addVIEw( v,0 );        v.setonScrollListener( new OnScrollListener()        {            @OverrIDe            public voID onScroll( AbsListVIEw vIEw,int firstVisibleItem,int visibleItemCount,int totalitemCount )            {                scrollposition[ position ] = firstVisibleItem;            }            @OverrIDe            public voID onScrollStateChanged(AbsListVIEw vIEw,int scrollState) {                // Todo auto-generated method stub            }        } );        return v;}    else {        TextVIEw v = new TextVIEw( context );        v.setText( Titles[ position ] );        ((VIEwPager)pager).addVIEw( v,0 );        return v;    }}@OverrIDepublic voID destroyItem( VIEw pager,int position,Object vIEw ){    if (position == 0){    ( (VIEwPager) pager ).removeVIEw( (ListVIEw) vIEw );}    else {    ( (VIEwPager) pager ).removeVIEw( (TextVIEw) vIEw );}}@OverrIDepublic boolean isVIEwFromObject( VIEw vIEw,Object object ){    return vIEw.equals( object );}@OverrIDepublic voID finishUpdate( VIEw vIEw ) {}@OverrIDepublic voID restoreState( Parcelable p,ClassLoader c ) {}@OverrIDepublic Parcelable saveState() {    return null;}@OverrIDepublic voID startUpdate( VIEw vIEw ) {}}

因此,使用ListVIEw示例一切都很好.

但是当我尝试将自定义ListVIEw放在那里时,我的问题就开始了.在我的旧项目中,这个ListVIEw有代码:

package com.formatbce.mdrive;import java.util.ArrayList;import androID.app.AlertDialog;import androID.app.ListActivity;import androID.app.ProgressDialog;import androID.content.Context;import androID.content.DialogInterface;import androID.content.Intent;import androID.os.AsyncTask;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.ListVIEw;public class Concerts extends ListActivity {private ArrayList<RSS_PostItem> messages;private RSS_PostAdapter adapter;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    if (savedInstanceState != null) {        messages = savedInstanceState.getParcelableArrayList("messages");        adapter = new RSS_PostAdapter(Concerts.this,R.layout.post_entry,messages);        setlistadapter(adapter);    } else        new GetParserResult().execute();}@OverrIDeprotected voID onListItemClick(ListVIEw l,VIEw v,long ID) {    super.onListItemClick(l,v,position,ID);    Intent postActivity = new Intent(this,RSS_PostActivity.class);    Bundle store = new Bundle();    store.putString("Title",messages.get(position).Title);    store.putString("description",messages.get(position).description);    store.putString("link",messages.get(position).link);    postActivity.putExtras(store);    startActivity(postActivity);}protected voID onSaveInstanceState(Bundle savedInstanceState) {    super.onSaveInstanceState(savedInstanceState);    savedInstanceState.putParcelableArrayList("messages",messages);}private class GetParserResult extends        AsyncTask<Context,Integer,ArrayList<RSS_PostItem>> {    private ProgressDialog loadingDialog;    private Concerts_Parser parser;    protected voID onPreExecute() {        parser = new Concerts_Parser();        loadingDialog = ProgressDialog.show(Concerts.this,"","Loading concerts List...",true);    }    @OverrIDe    protected ArrayList<RSS_PostItem> doInBackground(Context... arg0) {        return parser.parse();    }    protected voID onPostExecute(ArrayList<RSS_PostItem> result) {        if (result == null) {            AlertDialog.Builder builder = new AlertDialog.Builder(                    Concerts.this);            builder.setMessage(                    "Cannot load List... \nIs Internet ON?")                    .setCancelable(false)                    .setPositivebutton("Ok",new DialogInterface.OnClickListener() {                                public voID onClick(DialogInterface dialog,int ID) {                                    dialog.cancel();                                    loadingDialog.dismiss();                                }                            });            AlertDialog alert = builder.create();            alert.show();        } else {            messages = result;            loadingDialog.dismiss();            adapter = new RSS_PostAdapter(Concerts.this,messages);            setlistadapter(adapter);        }    }}   }

所以我不明白,如何在VIEwPager代码中正确使用它.@H_301_7@我尝试设置ListFragment,但对我来说这是一个非常新的,所以我无法让它工作.

Google也无法帮助我,就像各种开发人员论坛一样.

确实,问题是:如何在VIEwPager中显示自定义ListVIEw?

谢谢,我希望你能得到帮助.

UPD:

感谢第一个答案,我尝试将我的一个活动转换为片段,使用本教程:http://www.e-nature.ch/tech/?p=55.

首先,这是一个我无法通过的错误:这个变量没有转换,我不知道,如何让它工作.

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

现在另外一件事:当我试图将结果片段放到FragmentPagerAdapter时 – 我在这里找不到正确的代码:

@OverrIDepublic Fragment getItem(int arg0) {    // Todo auto-generated method stub    return ?????????;}

请告诉我,这种方法必须返回什么?@H_301_7@谢谢!

解决方法 你说

i have one TabHost with three tabs,and different ListVIEw in each tab

我假设你有三个不同的ListActivity,它传递给tabHost.所以将三个活动转换为三个碎片.

根据以下示例代码更改您的VIEwPagerAdapter

class GoogleMusicAdapter extends FragmentPagerAdapter implements        TitleProvIDer {    public GoogleMusicAdapter(FragmentManager fm) {        super(fm);    }    @OverrIDe    public Fragment getItem(int position) {                    if(position ==0 )        // return your Fragment1                     else if(position==1)                    // return your Fragment2                    else                    // return fragment3    }    @OverrIDe    public int getCount() {        return 3;    }    @OverrIDe    public String getTitle(int position) {        if(position ==0 )                    return "One"                    else if(position ==1)                    return "two"                    else                    return "three"    }}
@H_403_2@ 总结

以上是内存溢出为你收集整理的android – ViewPager指标与自定义ListView全部内容,希望文章能够帮你解决android – ViewPager指标与自定义ListView所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1126529.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存