TextSwitcher 的基本用法
TextSwitcher 字面理解是文字交换器,是ViewSwitcher的子类,从ViewSwitcher来看,是View交换器,TextSwitcher继承自ViewSwitcher,显然是交换TextView。
应用分为三步:
1.得到 TextSwitcher 实例对象
TextSwitcher switcher = (TextSwitcher) findViewById(R.id.textSwitcher);
2.为switcher指定ViewSwitcher.ViewFactory工厂,该工厂会产生出转换时需要的View
switcher.setFactory(this);
3.为switcher设定显示的内容,该方法执行,就会切换到下个View
switcher.setText(String.valueOf(new Random().nextInt()));
其中 要实现ViewSwitcher.ViewFactory中的makeView()方法
// 重写 ViewSwitcher.ViewFactory 的 makeView()方法,返回一个 View,TextSwitcher 交换时使用
@Override
public View makeView() {
TextView textView = new TextView(this);
textView.setTextSize(36);
return textView;
}
如果不适用ViewSwitcher.ViewFactory,也可以使用下面的方式代替
//如果不用switcher.setFactory()方法设置转换时的View,也可以调用两次switcher.addView(view,index,params);
//其中view为要切换的View,index为索引,params是添加时的宽,高参数
// TextView textView1 = new TextView(this);
// textView1.setTextSize(36);
// textView1.setTextColor(Color.RED);
// TextView textView2 = new TextView(this);
// textView2.setTextSize(36);
// textView2.setTextColor(Color.YELLOW);
// switcher.addView(textView1, 0,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
// switcher.addView(textView2, 1,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
实例代码:
public class MainFragment extends BaseFragment implements View.OnClickListener ,ViewSwitcher.ViewFactory{
private TextSwitcher main_ts;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
viewList = new ArrayList<>();
view = inflater.inflate(R.layout.firsrt_page,container, false);
main_ts = (TextSwitcher) view.findViewById(R.id.main_ts);
main_ts.setFactory(this);
Animation in = AnimationUtils.loadAnimation(getActivity(),
R.anim.move_left_in1_activity);
Animation out = AnimationUtils.loadAnimation(getActivity(),
R.anim.move_left_out1_activity);
main_ts.setInAnimation(in);
main_ts.setOutAnimation(out);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTask(), 1, 10000);
return view;
}
@Override
public View makeView() {
TextView tv = new TextView(getActivity());
tv.setTextSize(13);
tv.setTextColor(Color.BLACK);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
TextSwitcher.LayoutParams params = new TextSwitcher.LayoutParams(TextSwitcher.LayoutParams.MATCH_PARENT,TextSwitcher.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(params);
tv.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
tv.setLines(1);
return tv;
}
private class MyTask extends TimerTask {
@Override
public void run() {
getNotcieDate(Constants.GONG_GAO);
// getAccountDate(Constants.ITEMACCOUNT);
}
}
protected void getNotcieDate(final String url) {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
if(list.size()==0){
// TODO Auto-generated method stub
String subTokenCode;
try {
subTokenCode = HttpUtils.getSubTokenCode("",
BaseApplication.tokenCode, getActivity());
Log.i("获取的子票据", subTokenCode);
String new_url = url+"&token=" + subTokenCode;
Log.i("获取的新的url", new_url);
com.lidroid.xutils.HttpUtils httpUtils = new com.lidroid.xutils.HttpUtils();
httpUtils.send(HttpRequest.HttpMethod.POST, new_url,
new RequestCallBack<String>() {
@Override
public void onFailure(HttpException arg0,
String arg1) {
// mProgressBar.setVisibility(View.GONE);
// TODO Auto-generated method stub
// Toast.makeText(BaseApplication.getContext(), "失败", Toast.LENGTH_SHORT).show();
Log.d("失败原因", arg0.toString());
// pullToRefreshListView.onRefreshComplete();
}
@Override
public void onSuccess(ResponseInfo<String> arg0) {
// list.clear();
// mProgressBar.setVisibility(View.GONE);
String result = arg0.result;
// result = getResult();
Log.d("输出结果", result);
// Toast.makeText(BaseApplication.getContext(),"成功",Toast.LENGTH_SHORT).show();
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray jsonArray = jsonObject
.getJSONArray("rows");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray
.getJSONObject(i);
Gson gson = new Gson();
GongGaoListBean bean = gson.fromJson(
jsonObject2.toString(),
GongGaoListBean.class);
date = new Date(Long.valueOf(bean.getCREATE_DATE()));
calendar.setTime(date);
bean.setCREATE_DATE(formatter.format(calendar.getTime()).toString());
String context = bean.getCONTEXT();
context = context.replaceAll("<br>","");
bean.setCONTEXT(context);
list.add(bean);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Log.i("输出list的大小", "" + list.size());
if (list.size() != 0) {
Message message = new Message();
// list.get(num % list.size());
message.what = 0x24;
Log.d("num的余数",num % list.size()+"");
myAccount = num % list.size();
message.obj = list.get(num % list.size())
.getTITLE()+" "+list.get(num % list.size())
.getCREATE_DATE();
// message.obj =myAccount+"";
handler.sendMessage(message);
num++;
}
//ddd
}
}).start();
// progressbar.setVisibility(View.VISIBLE);
}
}
在anim中的动画效果:
move_left_in1_activity:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0%p">
</translate>
move_left_out1_activity:
<?xml version="1.0" encoding="utf-8"?>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:fromYDelta="0%p"
android:toYDelta="-100%p">
</translate>
firsrt_page:
<TextSwitcher
android:id="@+id/main_ts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
效果如下:
本文介绍了TextSwitcher组件的基本用法及其实现细节。通过三个步骤展示了如何使用TextSwitcher来实现文本视图的切换,并提供了具体的代码示例。此外,还介绍了如何设置动画效果以增强用户体验。
249

被折叠的 条评论
为什么被折叠?



