【问题标题】:How to make Horizontal line attach textview in recyclerview?如何在recyclerview中制作水平线附加textview?
【发布时间】:2018-01-22 06:52:20
【问题描述】:

我对如何在 recyclerview 中填充时如何使水平视图触摸 textview 有疑问,现在让我简要解释一下,我在适配器视图持有人中有一个水平 recyclerview 我在 textview 的末尾有一个水平视图居中垂直于该文本视图,因此当添加项目时,每个视图都必须像垂直虚线时间轴一样触及文本视图,但视图没有触及文本视图,如下图所示。

现在让我发布到目前为止我尝试过的内容

这是持有recyclerview的布局:

<RelativeLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/recyclerview"/>

</RelativeLayout> 

我在活动中使用该回收器视图:

  Recyclerview recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        recyclerView.setHasFixedSize(true);
        TrackingAdapter mAdapter = new TrackingAdapter(this.taskLogModelList,mTaskModel.getTaskID(),fm,String.valueOf(mLastLocation.getLatitude()),String.valueOf(mLastLocation.getLongitude()));
            recyclerView.setAdapter(mAdapter);
            mAdapter.notifyDataSetChanged();

这是正在使用的适配器:

public class TrackingAdapter extends RecyclerView.Adapter<TrackingAdapter.ViewHolder> {
   private List<TaskLogModel> taskStatusFlowModels;
    private D2DKnocks appclass;
   private String Lat,Long;
    private FragmentManager fragmentManager;
    private String TaskID;
    DateFormat date,msimpleDateformat;
    public TrackingAdapter(List<TaskLogModel> taskStatusFlowModels,String TaskID,FragmentManager context,String Lat,String Long) {
        this.taskStatusFlowModels = taskStatusFlowModels;
        this.fragmentManager=context;
         date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        msimpleDateformat = new SimpleDateFormat("hh.mm a");
        this.Lat=Lat;
        this.TaskID=TaskID;
        this.Long=Long;
         appclass=((D2DKnocks)D2DKnocks.getContext());
    }

    public class ViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{
        private AvertaTextview txt_time;
        private Rubik txt_delivered;
     //   private AvertaButton btn_start;
        private RelativeLayout layout;
        private View view_right;

        public ViewHolder(View itemView) {
            super(itemView);
            txt_time = itemView.findViewById(R.id.txt_time);
       //     btn_start=itemView.findViewById(R.id.start);
            layout=itemView.findViewById(R.id.layout);
            txt_delivered =  itemView.findViewById(R.id.txt_delivered);
            view_right = (View) itemView.findViewById(R.id.right_view_line);
            layout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            TaskLogModel taskLogModel=taskStatusFlowModels.get(getAdapterPosition());
            if(taskLogModel.getTaskStatusID()==0){
                TaskStatusDialog taskStatusDialog=new TaskStatusDialog();
                Bundle bundle=new Bundle();
                bundle.putInt("Size",taskStatusFlowModels.size());
                bundle.putString("Lat",Lat);
                bundle.putString("Long",Long);
                bundle.putString("TaskID",TaskID);
                taskStatusDialog.setArguments(bundle);
                taskStatusDialog.show(fragmentManager,"show");
            }

        }
    }

    @Override
    public TrackingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view_assignment_status, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(TrackingAdapter.ViewHolder holder, int position) {
             if (taskStatusFlowModels.get(position).getTaskStatusID() > 0) {
                StatusModel statusFlowModel = getStatusName(taskStatusFlowModels.get(position).getTaskStatusID());
                holder.txt_delivered.setText(statusFlowModel.getStatus());
                holder.txt_delivered.setBackgroundResource(R.drawable.btn_arrived_bg);
                 try {
                     Date uppdateDate=date.parse(taskStatusFlowModels.get(position).getUpdateDateTime());
                     String updatedDate=msimpleDateformat.format(uppdateDate);
                     String[] each = updatedDate.split(" ");
                     String str = each[1].replace("AM", "am").replace("PM","pm");

//                     SpannableString ss1=  new SpannableString(each[0]);
//                     SpannableString ss2=new SpannableString(each[1]);
//                     ss1.setSpan(new AbsoluteSizeSpan(20), 0, each[0].length(), SPAN_INCLUSIVE_INCLUSIVE);
//                     ss2.setSpan(new AbsoluteSizeSpan(15), 0, each[1].length(), SPAN_INCLUSIVE_INCLUSIVE);
//                     CharSequence finalText = TextUtils.concat(ss1, " ", ss2);
                    holder.txt_time.setText(each[0]+" "+str);
                 } catch (ParseException e) {
                     e.printStackTrace();
                 }
                 GradientDrawable gradientDrawable = (GradientDrawable) holder.txt_delivered.getBackground();
                gradientDrawable.setColor(Color.parseColor(statusFlowModel.getStatusColor()));
            } else {

                    holder.txt_delivered.setText("Choose");
                    holder.txt_delivered.setBackgroundResource(R.drawable.btn_choose_bg);
                    holder.txt_delivered.setTextColor(Color.parseColor("#b1b1b1"));
            }
            if(taskStatusFlowModels.size()-1==position){
                holder.view_right.setVisibility(View.GONE);
            }
            else {
                holder.view_right.setVisibility(View.VISIBLE);
            }
        }


    public StatusModel getStatusName(Integer statusID){
        for (int i=0;i<appclass.getStatusModelist().size();i++){
            if (appclass.getStatusModelist().get(i).getStatusID().equals(statusID)){
                return appclass.getStatusModelist().get(i);
            }

        }
        return null;
    }
    @Override
    public int getItemCount() {

        return taskStatusFlowModels.size();
    }
}

下面是视图持有者布局:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:orientation="vertical">


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layout">

        <com.trident.Hawkersky.service.CustomFonts.AvertaTextview
            android:id="@+id/txt_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:drawableLeft="@drawable/ic_checked"
            android:drawablePadding="10dp"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="10.00 am"
            android:textColor="#444444"
            android:textSize="12sp"
           />

        <View
            android:id="@+id/vertical_line"
            android:layout_width="30dp"
            android:layout_height="20dp"
            android:layout_centerHorizontal="true"
             android:layout_below="@+id/txt_time"
           android:background="@drawable/vertical_dotted_line"
            android:layerType="software"
            android:visibility="visible" />
<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_below="@+id/vertical_line"
    android:layout_height="wrap_content">
        <com.trident.Hawkersky.service.CustomFonts.Rubik
            android:id="@+id/txt_delivered"
            android:layout_centerHorizontal="true"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/thirty"
            android:layout_marginBottom="5dp"
            android:singleLine="true"

            android:padding="@dimen/seven"
            android:background="@drawable/btn_delivered_bg"
            android:gravity="center"
            android:text="Delivered"
            android:textAllCaps="true"
            android:textColor="#FFF"
            android:textSize="13sp"
            android:textStyle="bold" />

        <View
            android:id="@+id/right_view_line" <--- this is the view
            android:layout_width="@dimen/hundred"
            android:layout_height="5dp"
            android:layout_alignBottom="@+id/txt_delivered"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="12dp"
            android:layout_toEndOf="@+id/txt_delivered"
            android:layout_toRightOf="@+id/txt_delivered"
            android:background="@drawable/dotted"
            android:layerType="software"
            android:visibility="visible" />
</RelativeLayout>
    </RelativeLayout>
</RelativeLayout>

现在我需要的是我需要将该视图附加到 textview ,但我得到了上面的图像如何解决它。

【问题讨论】:

  • 你的问题不清楚。你能发布预期的输出吗?
  • 请检查图像,我需要在 viewholder 中连接该视图
  • 把你的问题整理一下,把activity的xml和itemView的xml放到一起。图片不解释太多..!那是水平线性布局还是什么?
  • 如果您提供一些清晰的详细信息,包括您尝试的代码以及您需要实现的目标,我们将尝试一下。
  • 请看看这些家伙

标签: android android-recyclerview


【解决方案1】:

请尝试一下,如果您需要进一步修改,请告知。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                             xmlns:app="http://schemas.android.com/apk/res-auto"
                                             android:id="@+id/layout"
                                             android:layout_width="wrap_content"
                                             android:layout_height="wrap_content"
                                             android:layout_marginTop="10dp"
                                             android:orientation="vertical">


    <com.trident.Hawkersky.service.CustomFonts.AvertaTextview
        android:id="@+id/txt_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="5dp"
        android:layout_marginStart="5dp"
        android:drawableLeft="@drawable/ic_checked"
        android:drawablePadding="10dp"
        android:gravity="center"
        android:text="10.00 am"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="@+id/txt_delivered"
        app:layout_constraintRight_toRightOf="@+id/txt_delivered"
        android:textColor="#444444"
        android:textSize="12sp"
        />

    <View
        android:id="@+id/vertical_line"
        android:layout_width="30dp"
        android:layout_height="20dp"
        app:layout_constraintLeft_toLeftOf="@+id/txt_delivered"
        app:layout_constraintRight_toRightOf="@+id/txt_delivered"
        android:layout_below="@+id/txt_time"
        android:layout_centerHorizontal="true"
        app:layout_constraintTop_toBottomOf="@+id/txt_time"
        android:background="@color/colorPrimaryDark"
        android:layerType="software"
        android:visibility="visible"/>

    <com.trident.Hawkersky.service.CustomFonts.Rubik
        android:id="@+id/txt_delivered"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/thirty"
        android:layout_marginBottom="5dp"
        android:background="@drawable/btn_delivered_bg"
        android:gravity="center"
        app:layout_constraintTop_toBottomOf="@+id/vertical_line"
        android:padding="@dimen/seven"
        android:singleLine="true"
        android:text="Delivered"
        android:textAllCaps="true"
        android:textColor="#FFF"
        android:textSize="13sp"
        android:textStyle="bold"/>

    <View
        android:id="@+id/right_view_line"
        android:layout_width="100dp"
        android:layout_height="5dp"
        app:layout_constraintTop_toTopOf="@+id/txt_delivered"
        app:layout_constraintBottom_toBottomOf="@+id/txt_delivered"
        app:layout_constraintLeft_toRightOf="@+id/txt_delivered"
        android:background="@drawable/dotted"
        android:layerType="software"
        android:visibility="visible"/>


</android.support.constraint.ConstraintLayout>

抱歉,我必须使用 ConstraintLayout。如果您之前没有使用过,请将此implementation 'com.android.support.constraint:constraint-layout:1.0.2'1 添加到您的模块级 build.gradle。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-07
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多