2017-07-27 90 views
0

我要实现的UI是Screenshot水平曲线列表视图

我想如下

<android.support.v4.view.ViewPager 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:id="@+id/vpSalary" 
     /> 

在我的Java代码,我设置这些属性视图寻呼机

使用视图寻呼机
vpSalary.setAdapter(new CustomPagerAdapter(getContext(),this)); 
vpSalary.setClipToPadding(false); 
vpSalary.setPadding(330,0,330,0); 
vpSalary.setCurrentItem(2); 

我的适配器是

public class CustomPagerAdapter extends PagerAdapter { 

    String salarylist[] = {"30,000","40,000","50,000","60,000"}; 
    private Context mContext; 
    private PagerCallBack mPagerCallBack; 
    private Fragment fragment; 
    public CustomPagerAdapter(Context context,Fragment fragment) { 
     mContext = context; 
     this.fragment=fragment; 
     mPagerCallBack= (PagerCallBack) fragment; 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position) { 
     //if Length of both array's (salarylist and tipler) Always remains same then we can do code like below. 

     LayoutInflater inflater = LayoutInflater.from(mContext); 
     ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.adapter_salary_list, collection, false); 
     TextView textView= (TextView) layout.findViewById(R.id.txtSalary); 
     textView.setTextColor(mContext.getResources().getColor(R.color.highlight_grey)); 
     collection.addView(layout); 
     collection.requestFocus(); 

     return layout; 
    } 

    @Override 
    public void destroyItem(ViewGroup collection, int position, Object view) { 
     collection.removeView((View) view); 
    } 

    @Override 
    public int getCount() { 
     return salarylist.length; 
    } 

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

    public interface PagerCallBack{ 
     void getPagerCallBack(View view); 
    } 
} 

之后我得到这enter image description here

我的问题是,我如何得到在我的片段焦点的页面视图? 我只是想改变有重点的TextView

回答

2

实现此目的的一种方法是将您的查看传呼机添加到OnPageChangeListener的 。当页面选择意味着在 onPageSelected方法每当位置变化更新您的适配器。

注:关于如何更新视图寻呼机适配器动态地看this

第1步:

class CustomPagerAdapter extends PagerAdapter { 

    private int mCurrentPosition; 
    //rest of your code 

    void setCurrentPosition(int position) { 
     mCurrentPosition = position; 
     notifyDataSetChanged(); 
    } 

    @Override 
    public Object instantiateItem(ViewGroup collection, int position) { 
     //if Length of both array's (salarylist and tipler) Always remains same then we can do code like below. 

     LayoutInflater inflater = LayoutInflater.from(mContext); 
     ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.adapter_salary_list, collection, false); 
     TextView textView= (TextView) layout.findViewById(R.id.txtSalary); 
     if(position == mCurrentPosition) { 
      textView.setTextColor(highlightedTextColor); 
     } else { 
      textView.setTextColor(defaultTextColor) 
     } 
     collection.addView(layout); 
     collection.requestFocus(); 

     return layout; 
    } 
} 

第2步:

CustomPagerAdapter adapter = new CustomPagerAdapter(getContext(),this); 
    vpSalary.setAdapter(adapter); 

    vpSalary.addOnPageChangeListener(new OnPageChangeListener() { 
     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

     } 

     @Override public void onPageSelected(int position) { 
      adapter.setCurrentPosition(position); 
     } 

     @Override public void onPageScrollStateChanged(int state) { 

     } 
    }); 
+0

谢谢对于你的解决方案,我不得不添加'@Override public int getItemPosition(Object object){ return PagerAdapter.POSITION_NONE; ',其余都很完美 –