android寻呼机滑动标签条片段更换器

Posted

技术标签:

【中文标题】android寻呼机滑动标签条片段更换器【英文标题】:android pager sliding tabstrip fragment changer 【发布时间】:2013-08-04 08:19:27 【问题描述】:

我想通过在带有片段的选项卡上滑动来更改布局。有人可以帮我解决我的问题吗? 这是我的片段活动:

public class MainActivity extends FragmentActivity 

    private final Handler handler = new Handler();

    private PagerSlidingTabStrip tabs;
    private ViewPager pager;
    private MyPagerAdapter adapter;

    private Drawable oldBackground = null;
    private int currentColor = 0xFF666666;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        pager = (ViewPager) findViewById(R.id.pager);
        adapter = new MyPagerAdapter(getSupportFragmentManager());

        pager.setAdapter(adapter);

        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);

        tabs.setViewPager(pager);

        changeColor(currentColor);
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 

        switch (item.getItemId()) 

        case R.id.action_contact:
            QuickContactFragment dialog = new QuickContactFragment();
            dialog.show(getSupportFragmentManager(), "QuickContactFragment");
            return true;

        

        return super.onOptionsItemSelected(item);
    

    private void changeColor(int newColor) 

        tabs.setIndicatorColor(newColor);

        // change ActionBar color just if an ActionBar is available
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 

            Drawable colorDrawable = new ColorDrawable(newColor);
            Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
            LayerDrawable ld = new LayerDrawable(new Drawable[]  colorDrawable, bottomDrawable );

            if (oldBackground == null) 

                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) 
                    ld.setCallback(drawableCallback);
                 else 
                    getActionBar().setBackgroundDrawable(ld);
                

             else 

                TransitionDrawable td = new TransitionDrawable(new Drawable[]  oldBackground, ld );

                // workaround for broken ActionBarContainer drawable handling on
                // pre-API 17 builds
                // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) 
                    td.setCallback(drawableCallback);
                 else 
                    getActionBar().setBackgroundDrawable(td);
                

                td.startTransition(200);

            

            oldBackground = ld;

            // http://***.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
            getActionBar().setDisplayShowTitleEnabled(false);
            getActionBar().setDisplayShowTitleEnabled(true);

        

        currentColor = newColor;

    

    public void onColorClicked(View v) 

        int color = Color.parseColor(v.getTag().toString());
        changeColor(color);

    

    @Override
    protected void onSaveInstanceState(Bundle outState) 
        super.onSaveInstanceState(outState);
        outState.putInt("currentColor", currentColor);
    

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) 
        super.onRestoreInstanceState(savedInstanceState);
        currentColor = savedInstanceState.getInt("currentColor");
        changeColor(currentColor);
    

    private Drawable.Callback drawableCallback = new Drawable.Callback() 
        @Override
        public void invalidateDrawable(Drawable who) 
            getActionBar().setBackgroundDrawable(who);
        

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when) 
            handler.postAtTime(what, when);
        

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) 
            handler.removeCallbacks(what);
        
    ;

    public class MyPagerAdapter extends FragmentPagerAdapter 

        private final String[] TITLES =  "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
                "Top New Free", "Trending" ;

        public MyPagerAdapter(FragmentManager fm) 
            super(fm);
        

        @Override
        public CharSequence getPageTitle(int position) 
            return TITLES[position];
        

        @Override
        public int getCount() 
            return TITLES.length;
        

        @Override
        public Fragment getItem(int position) 
            return SuperAwesomeCardFragment.newInstance(position);
        

    

这是我在文本视图中写入位置编号的片段:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

public class SuperAwesomeCardFragment extends Fragment 

    private static final String ARG_POSITION = "position";

    private int position;

    public static SuperAwesomeCardFragment newInstance(int position) 
        SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
        Bundle b = new Bundle();
        b.putInt(ARG_POSITION, position);
        f.setArguments(b);
        return f;
    

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);

        position = getArguments().getInt(ARG_POSITION);
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 

        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        FrameLayout fl = new FrameLayout(getActivity());
        fl.setLayoutParams(params);

        final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
                .getDisplayMetrics());

        TextView v = new TextView(getActivity());
        params.setMargins(margin, margin, margin, margin);
        v.setLayoutParams(params);
        v.setLayoutParams(params);
        v.setGravity(Gravity.CENTER);
        v.setBackgroundResource(R.drawable.background_card);
        v.setText("CARD " + (position + 1));




        fl.addView(v);
        return fl;
    

您可以在此处找到示例的库和完整代码:

https://github.com/astuetz/PagerSlidingTabStrip

【问题讨论】:

您的方法遇到了哪些问题?什么不起作用? 我想在滑动时更改布局视图,我不想在每个选项卡上只打印不同的文本 你得到答案了吗?我有同样的问题,需要非常紧急地解决。请问你有没有得到答案? 【参考方案1】:

我知道已经晚了,但可能会对某人有所帮助:

这里是快速修复:

先创建其他布局,假设你创建了3个:我也假设布局给出一个清晰的思路

layout/lay1.xml - has a textview only
layout/lay2.xml - has an imageview only
layout/lay3.xml - has both textview and imageview

现在在 oncreateView 中:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 

    // if else or switch case, your wish
    View view;
    if (position == 0) 
        // means lay1.xml
        view = inflater.inflate(R.layout.lay1, container, false);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText("Content not available");
        return view;

     else if (position == 1) 
        // means lay2.xml
        view = inflater.inflate(R.layout.lay2, container, false);

        ImageView imagebg = (ImageView) view.findViewById(R.id.image1);

        imagebg.setImageResource(R.drawable.ic_launcher);
        return view;

     else 
        // means lay3.xml
        view = inflater.inflate(R.layout.lay3, container, false);

        ImageView imagebg = (ImageView) view.findViewById(R.id.image1);
        imagebg.setImageResource(R.drawable.ic_launcher);

        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText("this is working wow, thanks.");

        return view;

    

【讨论】:

以上是关于android寻呼机滑动标签条片段更换器的主要内容,如果未能解决你的问题,请参考以下文章

底部的android寻呼机标签条带走整个片段

在视图寻呼机 2 中禁用垂直滚动

使用 ViewPager 滑动时,片段没有调用 OnResume()

滑动到下一个片段并返回后的片段不显示任何内容

将不透明度设置为滑动标签布局?

Android - 片段中的联系人选择器