0

嗨,我目前正在设计我的用户信息表单的样式,我可以问别人如何实现当我按下某个 imageview 按钮时,我的视图将展开,反之亦然。

示例屏幕截图:

如果不展开。

在此处输入图像描述

按向下箭头展开时:

在此处输入图像描述

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();
                }
            });
        }
    }
}

感谢任何帮助。

4

1 回答 1

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 行放在容器布局中,这样您只需 2 行代码就可以切换它们的可见性

<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>
于 2017-02-06T16:13:43.920 回答