在按下对象后将TextView文本更改为Normal

时间:2018-03-26 16:57:56

标签: java android custom-adapter android-typeface

我目前有一个ListView,它使用CustomAdapter显示Firebase数据库中的信息。如下所示:

ListView

创建后,我希望绿色框(ListView项目)中的文本为粗体。一旦用户按下该ListView项目,我希望文本转到Normal,unbolded。

我希望它的工作方式类似于电子邮件。当您收到新电子邮件时,标题是粗体的,一旦打开该电子邮件并返回收件箱,该文本就是正常的,未加盖的。

以下是我的所有相关代码:

MyJobsFragment,SelectItem

// Press the object and display the information and sign the job of with signature pad

    jobListViewMyAcJobs.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
            Bundle bundle = new Bundle();
            bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
            bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
            activeJobDetailsFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
        }
    });

MyCustomAdapter

public class MyCustomAdapter extends BaseAdapter
    {
        private ArrayList<JobInformation> mData = new ArrayList<>();
        private ArrayList<JobInformation> mDataOrig = new ArrayList<>();
        private ArrayList<String> mDataKeys = new ArrayList<>();

        private LayoutInflater mInflater;

        ... Removed Code ...

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            // Bid on holder
            MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn holderBidOn;
            // Accepted holder
            final MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted holderAccepted;
            // Completed holder
            MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted holderCompleted;

            if (convertView == null)
            {
                // Bid on
                if (host.getCurrentTab() == 0)
                {
                    convertView = mInflater.inflate(R.layout.job_info_bid_on, null);
                    holderBidOn = new MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn();

                    holderBidOn.textViewJobName = convertView.findViewById(R.id.textName);
                    holderBidOn.textViewJobDescription = convertView.findViewById(R.id.textDesc);

                    holderBidOn.textViewJobName.setText(mData.get(position).getAdvertName());
                    holderBidOn.textViewJobDescription.setText(mData.get(position).getAdvertDescription());

                    convertView.setTag(holderBidOn);
                }
                // Accepted
                else if (host.getCurrentTab() == 1)
                {
                    convertView = mInflater.inflate(R.layout.job_info_accepted, null);
                    holderAccepted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted();

                    holderAccepted.textViewJobName = convertView.findViewById(R.id.textName);
                    holderAccepted.textViewDescription = convertView.findViewById(R.id.textDesc);

                    holderAccepted.textViewJobName.setText(mData.get(position).getAdvertName());
                    if(text != null)
                    {
                        text.setTypeface(null, Typeface.BOLD);
                    }

                    holderAccepted.textViewDescription.setText(mData.get(position).getAdvertDescription());
                    // TODO - Was going to add the the users bid into this here, however it's difficult as the bid isnt stored in the jobs table
                    // TODO - I tried to add it in, looped through the bids table and found the bids with the mDataKeys. But it always displayed the last value.

                    convertView.setTag(holderAccepted);
                }
                // Completed
                else if (host.getCurrentTab() == 2)
                {
                    convertView = mInflater.inflate(R.layout.job_info_list_completed, null);

                    holderCompleted = new MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted();

                    holderCompleted.textViewJobName = convertView.findViewById(R.id.textName);
                    holderCompleted.textViewJobName.setText(mData.get(position).getAdvertName());

                    convertView.setTag(holderCompleted);
                }
            } else
            {
                if (host.getCurrentTab() == 0)
                {
                    holderBidOn = (MyJobsFragment.MyCustomAdapter.GroupViewHolderBidOn) convertView.getTag();
                } else if (host.getCurrentTab() == 1)
                {
                    holderAccepted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderAccepted) convertView.getTag();
                } else if (host.getCurrentTab() == 2)
                {
                    holderCompleted = (MyJobsFragment.MyCustomAdapter.GroupViewHolderCompleted) convertView.getTag();
                }
            }

            return convertView;
        }

        public class GroupViewHolderBidOn
        {
            public TextView textViewJobName;
            public TextView textViewJobDescription;
        }

        public class GroupViewHolderAccepted
        {
            public TextView textViewJobName;
            public TextView textViewDescription;
        }

        public class GroupViewHolderCompleted
        {
            public TextView textViewJobName;
        }
}

1 个答案:

答案 0 :(得分:0)

有多种方法可以实现这一目标。我建议你在JobInformation课程中添加一个字段,比如

public class JobInformation{
    .
    .
    .
    .
    . // other fields

    private boolean isSelected;

    public boolean isSelected() {
        return isSelected;
    }
    public void setSelected(boolean selected) {
        isSelected = selected;
    }

    .
    .
    . // Other getters/setters

}

现在您需要在单击某个项目时设置此字段,在onItemClick函数中执行此操作

public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            ActiveJobDetailsFragment activeJobDetailsFragment = new ActiveJobDetailsFragment();
            Bundle bundle = new Bundle();
            adapterActiveJobs.mData.get(position).setSelected(true); // add this line in your code
            bundle.putSerializable("Job", adapterActiveJobs.mData.get(position));
            bundle.putSerializable("JobId", adapterActiveJobs.mDataKeys.get(position));
            activeJobDetailsFragment.setArguments(bundle);
            getFragmentManager().beginTransaction().replace(R.id.content, activeJobDetailsFragment).addToBackStack(host.getCurrentTabTag()).commit();
        }

最后在自定义适配器的getView方法中,检查isSelected字段并相应地设置textview样式,并在getView方法中添加。

if(mData.get(position).isSelected()){
    // set your textview typeface to normal
}
else{
    // set your texview typeface to bold.
}

并且每当您更新adapter.notifyDataSetChanged()变量时都需要致电selected,以便调用getView