如何创建行之间具有不同背景的警报对话框列表

时间:2018-08-20 09:32:36

标签: java android android-alertdialog

如何在这样的警报对话框中设置背景奇数行和偶数行? image

AlertDialog.Builder builder = new AlertDialog.Builder(MenuRegister.this);
builder.setTitle("Pilih Tipe User");
builder.setItems(arrData, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int position) {
        spinerUserType.setText(profesi.get(position).getName());
        changeView(profesi.get(position).getId());
        role = profesi.get(position).getId();

        if (role.equalsIgnoreCase("4")) {
            loadSpesialis();
        }

    }
}).show();

4 个答案:

答案 0 :(得分:1)

您将必须使用builder.setView()实现自定义视图。

然后,例如,如果使用ListView,则只需使用以下内容:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ...;
    }
    convertView.setBackgroundColor(position % 2 ? Color.GREY : Color.WHITE);
    ...
}

cf https://stackoverflow.com/a/9697599/603270

答案 1 :(得分:0)

您可以设置自己的适配器,而不是设置项目。

AlertDialog.Builder builder = new AlertDialog.Builder(TestActivity.this);
    builder.setTitle("Pilih Tipe User");
    // Create an ArrayAdapter from List
    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, new String[]{"test1", "test2", "test3"}){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            // Get the current item from ListView
            View view = super.getView(position,convertView,parent);
            if(position %2 == 1)
            {
                // Set a background color for ListView regular row/item
                view.setBackgroundColor(Color.parseColor("#FFB6B546"));
            }
            else
            {
                // Set the background color for alternate row/item
                view.setBackgroundColor(Color.parseColor("#FFCCCB4C"));
            }
            return view;
        }
    };
    builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int position) {
        }
    }).show();

答案 2 :(得分:0)

在getView方法中

@NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.file_list_row, parent, false);

        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.layout_id);

    //set the background color of alternative row
            if (position % 2 == 0) {
                linearLayout.setBackgroundColor(Color.parseColor("#BDBDBD"));

            } else {
                linearLayout.setBackgroundColor(Color.parseColor("#EEEEEE"));
            }
return view;
}

在您的原始xml文件中

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

    <LinearLayout
        android:id="@+id/layout_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

/* Your Views */

    </LinearLayout>
</LinearLayout>

答案 3 :(得分:0)

您必须像这样从recyclerView和自定义适配器使用

private void selectCategoryDialog() {
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // before
    dialog.setContentView(R.layout.chips_dialog_layout);
    dialog.setCancelable(true);

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    RecyclerView recyclerView = dialog.findViewById(R.id.recyclerView);
    TextView dialogTitle = dialog.findViewById(R.id.dialog_title);

    dialogTitle.setText(getResources().getString(R.string.category));

    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    AdapterCategory _adapter = new AdapterCategory(this, placeCategories);
    recyclerView.setAdapter(_adapter);
    _adapter.setOnItemClickListener((view, obj, position) -> {
        addCategoryChip(obj);
        dialog.hide();
    });

    HelperFunctions.setFont(dialog
            .findViewById(android.R.id.content), this, Constants.iranSansFont);

    dialog.show();
    dialog.getWindow().setAttributes(lp);
}

适配器:

public class AdapterCategory extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private List<PlaceCategory> items = new ArrayList<>();


private OnItemClickListener mOnItemClickListener;
private Typeface mTypeface;

public interface OnItemClickListener {
    void onItemClick(View view, PlaceCategory obj, int position);
}

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mOnItemClickListener = mItemClickListener;
}

public AdapterCategory(Context context, List<PlaceCategory> items) {
    this.items = items;
    this.mTypeface = HelperFunctions.getTypeface(context);
}

public class OriginalViewHolder extends RecyclerView.ViewHolder {
    public ImageView image;
    public TextView title;
    public View lyt_parent;

    public OriginalViewHolder(View v) {
        super(v);
        image = v.findViewById(R.id.image);
        title = v.findViewById(R.id.type_title);
        lyt_parent = v.findViewById(R.id.lyt_parent);
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    RecyclerView.ViewHolder vh;
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.chips_item, parent, false);
    HelperFunctions.setFontForAdapter((ViewGroup) v, mTypeface);
    vh = new OriginalViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
    if (holder instanceof OriginalViewHolder) {
        OriginalViewHolder view = (OriginalViewHolder) holder;

        PlaceCategory p = items.get(position);
        view.title.setText(p.toString());
        view.lyt_parent.setOnClickListener(view1 -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(view1, items.get(position), position);
            }
        });

        view.lyt_parent.setBackgroundColor(position % 2 == 0 ? Color.GRAY : Color.WHITE);
    }
}

@Override
public int getItemCount() {
    return items.size();
}

对话框的自定义布局:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:padding="@dimen/spacing_large"
        android:text="Contact List"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/spacing_middle"
        android:scrollbars="vertical"
        android:scrollingCache="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</LinearLayout>

希望对您有帮助