左右导航到viewpager无法正常工作

时间:2015-04-15 09:43:51

标签: android android-viewpager viewpagerindicator

我在我的应用程序中使用view pager,viewpager工作正常但我想添加viewpagerindicator,以下是我的代码片段和xi的ui设计可以任何人告诉我什么是问题吗?在此先感谢..... ........

 public class Test_Pager extends Activity{
private String strtd;
String[] imgStr;
ImageView imageView;
ArrayList<String> userImgArrayList;
String[] myURLs;
/*country list*/
JSONArray country_list=null;
private ServiceHandler sh;
private String jsonStr;
private JSONObject jsonObj;
private String user_img;
private String user_pro;
private static String PROFILE_VIEW_URL = "";
private static final String USER_IMG="product_images";
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.test_pagerss);
    userImgArrayList = getIntent().getStringArrayListExtra("user_images");
    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
       ImageAdapter adapter = new ImageAdapter(this);
       viewPager.setAdapter(adapter);
      // TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
      // titleIndicator.setViewPager(viewPager);
       leftss=(ImageView)findViewById(R.id.lefticonsss);
       rightss=(ImageView)findViewById(R.id.righticonsss); 

     imageView = (ImageView) findViewById(R.id.full_image_view);
     PROFILE_VIEW_URL="";
    StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    sh = new ServiceHandler();

        // Making a request to url and getting response
        jsonStr = sh.makeServiceCall(PROFILE_VIEW_URL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

     try {
         jsonObj = new JSONObject(jsonStr);


             /*proview_business = jsonObj.getString(PROFILE_VIEW_BUSINESS);
             proview_subcat = jsonObj.getString(PROFILE_VIEW_SUB_CATAGORY);
             proview_mainpro = jsonObj.getString(PROFILE_VIEW_MAINPRODUCTS);
             proview_expr = jsonObj.getString(PROFILE_VIEW_EXPERIENCE);
             proview_cmpname = jsonObj.getString(PROFILE_VIEW_COMPANYNAME);
             proview_website = jsonObj.getString(PROFILE_VIEW_WEBSITE);*/

            // user_img=jsonObj.getString(USER_IMG);



            user_img=jsonObj.getString(USER_IMG);
            user_img = "";
            userImgArrayList = new ArrayList<String>();//declare userImgArrayList globally like ArrayList<String> userImgArrayList;
            JSONArray picarray = jsonObj.getJSONArray(USER_IMG);
            for(int i=0;i< picarray.length();i++)
            {
                user_img = picarray.getString(i);
                userImgArrayList.add(user_img);
                Log.d("mylog", "curent  pro pic  = " + user_img);
            }


     } catch (JSONException e) {
         e.printStackTrace();
     }
leftss.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                viewPager.setCurrentItem(+1,true);
            }
        });
}
public class ImageAdapter extends PagerAdapter {
     Context context;
      ImageAdapter(Context context)
     {
     this.context=context;
     }
      @Override
      public int getCount() {
      return USER_IMG.length();
      }

      @Override
        public void destroyItem(View container, int position, Object object) {
             ((ViewPager) container).removeView((View) object);
        }
     @Override
     public boolean isViewFromObject(View view, Object object) {
     return view == ((ImageView) object);
     }

     @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(
                    R.dimen.activity_horizontal_margin);
            imageView.setPadding(padding, padding, padding, padding);
            //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            //Picasso.with(context).load(userImgArrayList.get(position)).into(imageView);
            Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);

            /*for(int i=0; i<myURLs.length;i++)
            { 

                try {
                    url = new URL(myURLs[i]);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                try {
                    bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 imageView.setImageBitmap(bmp);
            }
            */
            // imageView.setImageURI(Uri.parse(imgStr[position]));
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }
     }

  }

Myxml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@android:color/black"
android:gravity="center_horizontal"
>



     <ImageView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"

   android:src="@drawable/arrow"
   android:rotation="180"
    android:id="@+id/lefticonsss"
    />


  <android.support.v4.view.ViewPager
  android:id="@+id/view_pager"
  android:layout_width="150dp"
  android:layout_height="150dp"

   />

    <ImageView android:id="@+id/full_image_view"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"/>


    <ImageView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/arrow"

    android:id="@+id/righticonsss"
    />



    </LinearLayout>

3 个答案:

答案 0 :(得分:0)

我使用此库https://github.com/astuetz/PagerSlidingTabStrip

摘要:

1.在您的onCreate

中执行此操作
// Initialize the ViewPager and set an adapter
ViewPager pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new TestAdapter(getSupportFragmentManager()));

// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
  1. (可选)如果您对OnPageChangeListener使用ViewPager,则应在Widget而不是ViewPager直接设置{。}}。

    tabs.setOnPageChangeListener(mPageChangeListener);

答案 1 :(得分:0)

您好我看到您尝试使用TitlePageIndicator的更新代码。我假设你确实实现了com.jakewharton.android.viewpagerindicator.TitlePageIndicator

我已更新您的代码,请参阅下面修改后的代码。

在您PagerAdapter确保您使用override the getTitle(int position)方法时,您必须向页面适配器提供正确的数据源。 getTitle(int position)从给定的数据源中查找标题文本。

public class ImageAdapter extends PagerAdapter implements TitleProvider
{ 
    private final Context context;

    public ImageAdapter( Context context )
    { 
        this.context = context;
    } 


    @Override 
    public String getTitle( int position )
    { 
        return userImgArrayList.get(position);
    } 


    @Override 
    public int getCount() 
    { 
        return userImgArrayList.size();
    } 


    @Override 
    public Object instantiateItem( View pager, final int position )
    { 
          ImageView imageView = new ImageView(context);
          int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
          imageView.setPadding(padding, padding, padding, padding);
          Picasso.with(context).load(userImgArrayList.get(position)).resize(200, 200) .into(imageView);
          ((ViewPager) container).addView(imageView, 0);
          return imageView;
    } 


    @Override 
    public void destroyItem( View pager, int position, Object view )
    { 
        ( (ViewPager) pager ).removeView( (ListView) view );
    } 


    @Override 
    public boolean isViewFromObject( View view, Object object )
    { 
        return view == ((ImageView) object);
    } 

} 

如果您有任何进一步的询问,请告诉我。谢谢

答案 2 :(得分:0)

Jake Wharton的{p> ViewPagerIndicator是一个cusomized View,与其他观点没有区别。所以你可以像使用任何其他View一样使用它。当然,ViewPagerIndicator有自己的一些自定义属性,如attrs。以你的片段为例,我认为你做得很好。
PS:试试这个:

<LinearLayout 
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/lnr11"
android:orientation="vertical"
>
<com.viewpagerindicator.TitlePageIndicator
 android:id="@+id/titles"
  android:layout_height="30dp"
  android:layout_width="match_parent" />

  <android.support.v4.view.ViewPager
  android:id="@+id/view_pager"
  android:layout_width="150dp"
  android:layout_height="150dp"
   />

    <ImageView android:id="@+id/full_image_view"
    android:layout_width="wrap_content"
    android:src="@drawable/ic_launcher"
    android:layout_height="wrap_content"/></LinearLayout>