我有一个大问题(对我而言) 我在该类的RecycleViewHolder.class中有一些来自api的项目我告诉那些项目的xml文件在哪里。现在我的问题是因为我在mainactivity中有首选项设置,当我将整个应用程序的背景颜色定义为黑色时,我的文本将是在相同的颜色“黑色”,因为颜色我在应用程序中看不到任何东西。现在我需要你的帮助如何在mainactivity中改变textview的颜色,当我尝试在MainActivity中调用findviewbyid并调用方法setTextColor时,我得到nullpointexception,因为它已经连接到RecycleViewHolder类中的xml。 这是RecyclceViewHolder类,其中包含我希望从主要活动中更改颜色的价格文本视图。
package com.example.domo.cryptotrace;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import android.view.View;
/**
* Created by Antun on 31.1.2018..
*/
public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView name, symbol, rank, percentChange1h, percentChange24h, percentChange7d;
public TextView price;
public RecyclerViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
name = itemView.findViewById(R.id.name);
symbol= itemView.findViewById(R.id.symbol);
rank = itemView.findViewById(R.id.
rank);
price=itemView.findViewById(R.id.price);
percentChange1h=itemView.findViewById(R.id.percent_change_1h);
percentChange24h=itemView.findViewById(R.id.percent_change_24h);
percentChange7d=itemView.findViewById(R.id.percent_change_7d);
}
@Override
public void onClick(View view) {
}
}
这是主要的活动(在这堂课我想改变 RecyclerViewHolder - > ItemView控件 - >价格颜色)
package com.example.domo.cryptotrace;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.View;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Button;
import com.crashlytics.android.Crashlytics;
import java.util.List;
import io.fabric.sdk.android.Fabric;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
public Button register;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Crashlytics());
final Fabric fabric = new Fabric.Builder(this)
.kits(new Crashlytics())
.debuggable(true)
.build();
Fabric.with(fabric);
setContentView(R.layout.activity_main);
getUserList();
register=findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LoginSys.class);
startActivity(intent);
}
});
}
@Override
protected void onResume() {
super.onResume();
getPreference();
}
//Ovo je klasa koju smo sami gore kreirali kako bi mogli odrediti boje od pozadine kroz Settings
//Tu još možeš i mjenjati text boju od texViewa sa metodom ||myTextView.setTextColor(0xAARRGGBB);||
//Ili ovako ||ContextCompat.getColor(context, R.color.your_color);||
private void getPreference() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
if (preferences.contains("color")) {
if (preferences.getString("color", "0").equals("1")) {
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
} else if (preferences.getString("color", "0").equals("2")) {
getWindow().getDecorView().setBackgroundColor(Color.BLACK);
} else if (preferences.getString("color","0").equals("3")){
getWindow().getDecorView().setBackgroundColor(Color.RED);
}else{
getWindow().getDecorView().setBackgroundColor(Color.MAGENTA);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.your_item_id) {
//A ovo je metoda za pozivanje i startanje preferences activity-a
Intent intent = new Intent(MainActivity.this, PreferencesScreenActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private void getUserList() {
try {
APIService service = ApiClient.getRetrofit().create(APIService.class);
Call<List<User>> call = service.getUserData();
call.enqueue(new Callback<List<User>>() {
@Override
public void onResponse(Call<List<User>> call, Response<List<User>> response) {
//Log.d("onResponse", response.message());
List<User> userList = response.body();
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(layoutManager);
RecyclerViewAdapter recyclerViewAdapter =
new RecyclerViewAdapter(userList);
recyclerView.setAdapter(recyclerViewAdapter);
}
@Override
public void onFailure(Call<List<User>> call, Throwable t) {
}
});
}catch (Exception e) {}
}
}
再一次, 我需要代码来改变RecyclerViewHolder类中textview项目价格的颜色。
感谢所有的帮助,每一个好的答案都将归功于谢谢!
答案 0 :(得分:0)
我没有真正进入您的代码,但我认为您可以在.xml文件中创建另一个TextView,其ID为“priceColored”或类似内容。
当您想要更改价格的颜色时,您只需执行以下操作:
price = itemView.findViewById(R.id.priceColored);
ID为priceColored的TextView将具有您要使用的颜色。如果您需要弄清楚如何在android中创建某种颜色的文本视图,您可以在线轻松找到它。
希望它有所帮助,
答案 1 :(得分:0)
使用color作为recyclerview构造函数:
int color;
RecyclerViewAdapter recyclerViewAdapter =
new RecyclerViewAdapter(this, userList, color);
//////////////////////////////////////////////////////////////////////////
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder>{
private Context context;
public static List<UserList> userList;
private int color;
public UserAdapter(Context context, List<UserList> userList, int color){
super();
//Getting all the superheroes
this.color = color;
this.userList = userList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_layout, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.colorText.setText(mColorText.setText);
//here you will change the text color from mainactivity
// this change color
holder.colorText.setTextColor(ContextCompat.getColor(context,color));
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView colorText;
public ViewHolder(View itemView) {
super(itemView);
context = itemView.getContext();
}
}
答案 2 :(得分:0)
在RecyclerViewHolder类中,使用颜色参数创建一个公共方法,并在与该类对象的mainactivity中使用该方法,并调用该公共方法并传递颜色代码。
在RecyclerViewHolder中创建以下方法。
window.localStorage.clear();
并在mainActivity中调用此方法
public void changeColor(int color){
price.setTextColor(color)
}