如何在android中单击永久更改文本颜色

时间:2018-01-16 11:13:13

标签: android

我想知道如何更改我的默认文字颜色,当我点击文字时,它设置默认颜色蓝色和我没有点击它的文字显示黑色。

此外,当我重新打开我的应用程序时,它会向我显示点击文字蓝色和非点击文字黑色。

我已经添加了文字蓝色,但是当我重新打开应用程序时,它会重置原样。

所以请告诉我该怎么做。

FileName: RecyclerViewAdepter.java

package com.ejobbox.ejobbox;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;

public class RecyclerViewAdapter extends RecyclerView.Adapter{
    private final int VIEW_TYPE_ITEM = 0;
    private final int VIEW_TYPE_LOADING = 1;
    private OnLoadMoreListener onLoadMoreListener;
    private boolean isLoading;
    private Activity activity;
    private ArrayList<Model> dataset;
    private Context mContext;
    private int visibleThreshold = 5;
    private int lastVisibleItem, totalItemCount;

    public RecyclerViewAdapter(RecyclerView recyclerView, ArrayList<Model> mlist, Context context) {
        this.dataset=mlist;
        this.mContext=context;
        this.activity=activity;


        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                    if (onLoadMoreListener != null) {
                        onLoadMoreListener.onLoadMore();
                    }
                    isLoading = true;
                }
            }
        });
    }

    public void setOnLoadMoreListener(OnLoadMoreListener mOnLoadMoreListener) {
        this.onLoadMoreListener = mOnLoadMoreListener;
    }

    @Override
    public int getItemViewType(int position) {
        return dataset.get(position) == null ? VIEW_TYPE_LOADING : VIEW_TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_ITEM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.postdetails,parent,false);
            return new ImageTypeViewHolder(view);
        } else if (viewType == VIEW_TYPE_LOADING) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemload,parent,false);
            return new LoadingViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ImageTypeViewHolder) {
            final Model object=dataset.get(position);
            final ImageTypeViewHolder imageTypeViewHolder=(ImageTypeViewHolder) holder;
            imageTypeViewHolder.title.setText(object.title);
            imageTypeViewHolder.subtitle.setText(object.subtitle);
            imageTypeViewHolder.link.setText(object.link);
            imageTypeViewHolder.date.setText(object.date);

            // Set On Click Listner
            imageTypeViewHolder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    imageTypeViewHolder.title.setTextColor(Color.BLUE);

                    Intent intent=new Intent(mContext, WPPostDetails.class);
                    intent.putExtra("itemPosition",position);

                    intent.putExtra("link",object.link);

                    mContext.startActivity(intent);
                }
            });


        } else if (holder instanceof LoadingViewHolder) {
            LoadingViewHolder loadingViewHolder = (LoadingViewHolder) holder;
            loadingViewHolder.progressBar.setIndeterminate(true);
        }
    }

    public static class ImageTypeViewHolder extends RecyclerView.ViewHolder{
        TextView title,subtitle,link,date;
        ImageView imageView;

        public ImageTypeViewHolder(View itemView){
            super(itemView);
            this.title=(TextView)itemView.findViewById(R.id.title);
            this.link=(TextView)itemView.findViewById(R.id.link);
            this.subtitle=(TextView) itemView.findViewById(R.id.subtitle);
            this.imageView=(ImageView) itemView.findViewById(R.id.icon);
            this.date=(TextView)itemView.findViewById(R.id.date);
        }
    }

    @Override
    public int getItemCount() {
        return dataset == null ? 0 : dataset.size();
    }

    public void setLoaded() {
        isLoading = false;
    }

    private class LoadingViewHolder extends RecyclerView.ViewHolder {
        public ProgressBar progressBar;

        public LoadingViewHolder(View view) {
            super(view);
            progressBar = (ProgressBar) view.findViewById(R.id.progressBar1);
        }
    }


}

1 个答案:

答案 0 :(得分:1)

你可以看一下android为app开发提供的SharedPreferences。它允许您存储/编辑有关您的应用程序的数据,这些数据会持续存在。下面是一个教程的链接,它曾帮助过我:

https://www.journaldev.com/9412/android-shared-preferences-example-tutorial