Android中的展开式视图

时间:2017-02-06 13:26:02

标签: android expandablerecyclerview

您好,我目前正在设计用户信息表单的样式,当我按某个图像查看按钮时,我可以问某人如何实现我的视图会扩展,反之亦然。

屏幕截图示例:

如果没有扩展。

enter image description here

按向下箭头展开:

enter image description here

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import com.yoesuv.demorecyclerview.adapter.CardViewAdapter;

public class MainActivity extends AppCompatActivity {

    private String[] dataset;
    private RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dataset = getResources().getStringArray(R.array.android);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if(getSupportActionBar()!=null){
            getSupportActionBar().setElevation(5);
        }

        RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        adapter = new CardViewAdapter(dataset);
        mRecyclerView.setAdapter(adapter);
    }
}

CardviewAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.yoesuv.demorecyclerview.R;

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

    private String[] dataset;

    public CardViewAdapter(String[] dataset){
        this.dataset = dataset;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_row, null);
        return new ViewHolder(itemLayout);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.tV.setText(dataset[position]);
    }

    @Override
    public int getItemCount() {
        return dataset.length;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        public TextView tV;

        public ViewHolder(View itemView) {
            super(itemView);

            tV = (TextView) itemView.findViewById(R.id.textView_data);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(v.getContext(), tV.getText().toString(), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您可以隐藏它,然后点击

再次显示
RelativeLayout hiddenLayout = (RelativeLayout) itemView.findViewById(R.id.hiddenLayout);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(hiddenLayout.getVisibility() == View.VISIBLE) hiddenLayout.setVisibility(View.GONE); // hides layout
                else hiddenLayout.setVisibility(View.VISIBLE); // shows layout


            }
        });

我建议在容器布局中放置一行Occupaction和Phone行,这样你就可以用两行代码切换它们的可见性

<RelativeLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="@+id/hiddenLayout" >
// put TextViews and EditTexts for Phone and Occupation here
</RelativeLayout>