【问题标题】:Horizontal Curved list view水平弯曲列表视图
【发布时间】:2017-07-27 19:07:10
【问题描述】:

我要实现的UI是

我正在尝试使用如下视图寻呼机

<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);
    }
}

之后我得到了这个

我的问题是,如何让页面视图在我的片段中成为焦点? 我只想更改具有焦点的TextView

【问题讨论】:

    标签: android android-layout android-viewpager


    【解决方案1】:

    实现此目的的一种方法是将您的视图寻呼机添加到 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) {
    
            }
        });
    

    【讨论】:

    • 感谢您的解决方案,我不得不添加@Override public int getItemPosition(Object object){ return PagerAdapter.POSITION_NONE; },其余的都很完美
    猜你喜欢
    • 2014-09-21
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多