公告内容切换

1


xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".test.TestActivity">

android:id="@+id/textSwitcher1"
android:layout_width="match_parent"
android:layout_height="wrap_content">


2

package com.ruhai.yibei.test;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.ruhai.yibei.Constant;
import com.ruhai.yibei.R;
import com.ruhai.yibei.bean.AnnouncementBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TestActivity extends AppCompatActivity {
private TextSwitcher textSwitcher;
List texts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test3);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);

textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
Log.e("wy", "makeView: ");
TextView tv = new TextView(TestActivity.this);
tv.setTextColor(Color.BLACK);
return tv;
}
});
texts = new ArrayList<>();
for (int i = 0; i < 10; i++) {
texts.add("循环....."+i);
}
textSwitcher.setText(texts.get(0));

Timer mTimer = new Timer();
final int[] j = {0};
mTimer.schedule(new TimerTask() {
@Override
public void run() {
// mHandler.sendEmptyMessage(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
j[0]++;
textSwitcher.setText(texts.get(j[0]));

}
});
}
}, 0, 3000);


}
}

3
往往需要联网滚动显示公告内容

package com.ruhai.yibei.test;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.ruhai.yibei.Constant;
import com.ruhai.yibei.R;
import com.ruhai.yibei.bean.AnnouncementBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class TestActivity extends AppCompatActivity {
private TextSwitcher textSwitcher;
List texts;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test3);
textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher1);

textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
Log.e("wy", "makeView: ");
TextView tv = new TextView(TestActivity.this);
tv.setTextColor(Color.BLACK);
return tv;
}
});
// texts = new ArrayList<>();
// for (int i = 0; i < 10; i++) {
// texts.add("循环....."+i);
// }
// textSwitcher.setText(texts.get(0));

// Timer mTimer = new Timer();
// final int[] j = {0};
// mTimer.schedule(new TimerTask() {
// @Override
// public void run() {
mHandler.sendEmptyMessage(1);
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// j[0]++;
// textSwitcher.setText(texts.get(j[0]));
//
// }
// });
// }
// }, 3000, 3000);


RequestParams params = new RequestParams(Constant.announcement);
params.addHeader("Authorization", Constant.authorization);
params.addHeader("language", "zh_CN");
params.setAsJsonContent(true);
x.http().get(params, new Callback.CommonCallback() {
@SuppressLint("ResourceAsColor")
@Override
public void onSuccess(String result) {
AnnouncementBean professionInfo = JSON.parseObject(result, AnnouncementBean.class);
Log.e("wy", "getTitle: " + professionInfo.getData().get(0).getTitle());

if (professionInfo.getCode().equals("00000000")) {
Timer mTimer1 = new Timer();
final int[] j = {0};
mTimer1.schedule(new TimerTask() {
@Override
public void run() {
// mHandler.sendEmptyMessage(1);
runOnUiThread(new Runnable() {
@Override
public void run() {
j[0]++;
if (j[0] >= professionInfo.getData().size()) {
j[0] = 0;
}
Log.e("wy", "127getTitle: " + professionInfo.getData().get(j[0]).getTitle());
textSwitcher.setText(professionInfo.getData().get(j[0]).getTitle());

}
});
}
}, 3000, 3000);


} else {
Toast.makeText(x.app(), professionInfo.getMsg(), Toast.LENGTH_LONG).show();
}


}

@Override
public void onError(Throwable ex, boolean isOnCallback) {
Log.e("wy", "result: " + ex.getMessage());
}

@Override
public void onCancelled(CancelledException cex) {
Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
}

@Override
public void onFinished() {

}
});


}
}

4 在fragment中会出现公告内容不显示问题,一定要对textView设置显示颜色

TextView tv = new TextView(TestActivity.this);
tv.setTextColor(Color.BLACK);

5 没有颜色分析思路:看看textview有没有内容,打印其宽高 tv.getHeight(),如果有宽高,再设置其颜色