gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:57:52 25 4
gpt4 key购买 nike

我想通过在带有 fragment 的选项卡上滑动来更改布局。谁能帮我解决我的问题?这是我的 fragment Activity :

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://stackoverflow.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);
}

}
}

这是我在 TextView 中写入位置编号的 fragment :

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

最佳答案

我知道它晚了但可能会帮助某人:

这是快速修复:

首先创建其他布局,让我们假设您创建 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 寻呼机滑动标签条 fragment 更换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18027443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com