设置视图可见性== GONE从android中的另一个视图

时间:2015-08-23 19:39:24

标签: android android-layout android-view

我是Android开发的新手,从java背景开始,我正在开发一个Android应用程序,我必须显示用户有的邀请,所以布局将如下所示

enter image description here

此处邀请响应视图(是,否,可能)将在点击每个邀请视图时显示,但我希望当用户点击另一个视图时,响应视图将关闭或者visibility = Gone。当点击邀请视图时,会显示当前的响应视图。

所以要解决这个问题,我已经在每个响应视图中添加了id(inviteId),如下所示

    final LinearLayout second = (LinearLayout) inviteView.findViewById(R.id.hidden);
    second.setId((int) currentInviteId);

现在我想在用户点击下一个邀请视图并尝试将第一个响应视图设置为" GONE"

时首先打开响应视图ID
public class InvitationFragment extends Fragment {

    private List<String> eventName = new ArrayList<>();
    private List<Long> eventId = new ArrayList<>();
    private List<String> eventPlace = new ArrayList<>();
    private List<EventMO> eventMOs = new ArrayList<>();
    private List<UserMO> userMO = new ArrayList<>();
    private Context context;
    private UserOccasions userOccasions;
    private UserDelegate userDelegate = new UserDelegate();
    private EventDelegates eventDelegates = new EventDelegates();
    private Gson gson = new Gson();
    private ProgressDialog prgDialog;
    private EventMO eventMO;
   // private long compareEventId;
    private SharedPreferences prefs;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final View view = inflater.inflate(R.layout.invitation_tab, container, false);
        context = getActivity().getApplicationContext();
        prgDialog = new ProgressDialog(getActivity());
        eventId.clear();
        eventName.clear();
        eventPlace.clear();
        // Set Progress Dialog Text
        prgDialog.setMessage("Please wait...");
        // Set Cancelable as False
        prgDialog.setCancelable(false);
        prgDialog.show();

        DatabaseHelper dbHelper = new DatabaseHelper(context);
        final UserMO userMO = dbHelper.getRingeeUserData(1);
        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... arg0) {
                return eventDelegates.getAllEventForUser(userMO, context);
            }

            @Override
            protected void onPostExecute(String eventlists) {
                if (eventlists != "null") {
                    eventMOs = gson.fromJson(eventlists, new TypeToken<List<EventMO>>() {
                    }.getType());
                    Toast.makeText(context, "total items of eventMo" + eventMOs.size(), Toast.LENGTH_LONG).show();

                    for (EventMO eventMO : eventMOs) {
                        eventName.add(eventMO.getText());
                        eventId.add(eventMO.getEventId());
                        eventPlace.add(eventMO.getPlace());
                    }

                    DatabaseHelper dbHelper = new DatabaseHelper(context);
                    //long totalInsertion = dbHelper.insertUserRelationTable(userMOs);
                    prgDialog.dismiss();
                    //Toast.makeText(context, "total userMos size " + userMOs.size() + "total db insertion size " + totalInsertion, Toast.LENGTH_LONG).show();
                    ListView occasionView = (ListView) view.findViewById(R.id.invitation_list_view);
                    userOccasions = new UserOccasions();
                    occasionView.setAdapter(userOccasions);
                    occasionView.setItemsCanFocus(false);
                    occasionView.setTextFilterEnabled(true);
                    occasionView.setOnItemClickListener(occasionView.getOnItemClickListener());
                }
            }
        }.execute(null, null, null);
        return view;
    }


    private class UserOccasions extends BaseAdapter {
        LayoutInflater mInflater;
        TextView eventNameTxtV, eventPlaceTxtV;

        UserOccasions() {
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return eventMOs.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        // show list values name and mobile number in contact page
        @Override
        public View getView(final int position,View inviteView, ViewGroup parent) {

            if (inviteView == null) {
                inviteView = mInflater.inflate(R.layout.invitation, null);
            }
            EventMO eventMO = eventMOs.get(position);
            final long currentEventId = eventMO.getEventId();

            eventNameTxtV = (TextView) inviteView.findViewById(R.id.invitation_title);
            eventPlaceTxtV = (TextView) inviteView.findViewById(R.id.invitation_place);
            eventNameTxtV.setText(eventMO.getText());
            eventPlaceTxtV.setText(eventMO.getPlace());
            inviteView.setTag(position);
            View v = inviteView.findViewById(R.id.invitation_single);

            final LinearLayout first = (LinearLayout) inviteView.findViewById(R.id.invitation_single);

            Button yesBtn = (Button) inviteView.findViewById(R.id.yesbutton);
            Button noBtn = (Button) inviteView.findViewById(R.id.nobutton);
            Button mayBeBtn = (Button) inviteView.findViewById(R.id.buttonmaybe);

            final LinearLayout second = (LinearLayout) inviteView.findViewById(R.id.hidden);
            second.setId((int) currentEventId);
            // to store current event id into shared preference, to compare event ids and close child layout if ids are differents
            prefs = context.getSharedPreferences(InvitationFragment.class.getSimpleName(), Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong("compareEventId", currentEventId);
            editor.commit();

            yesBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    second.setVisibility(View.GONE);
                    final String response = "yes";
                    final EventMO event = new EventMO();
                    event.setIs_Attend(response);

                    new AsyncTask<Void, Void, String>() {
                        protected String doInBackground(Void... arg0) {
                            return eventDelegates.updateEvent(event, context);

                        }

                    }.execute(null, null, null);

                }

            });
            noBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    second.setVisibility(View.GONE);
                    final String response = "no";
                    final EventMO event = new EventMO();
                    event.setIs_Attend(response);

                    new AsyncTask<Void, Void, String>() {
                        protected String doInBackground(Void... arg0) {
                            return eventDelegates.updateEvent(event, context);

                        }

                    }.execute(null, null, null);
                }
            });
            mayBeBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    second.setVisibility(View.GONE);
                    final String response = "maybe";
                    final EventMO event = new EventMO();
                    event.setIs_Attend(response);

                    new AsyncTask<Void, Void, String>() {
                        protected String doInBackground(Void... arg0) {
                            return eventDelegates.addEvent(event, context);


                        }

                    }.execute(null, null, null);
                }
            });


            first.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View invitationView) {
                    final long compareEventId = prefs.getLong("compareEventId", 0);
                    final long currentEventId = second.getId();

                    if(compareEventId != 0 && compareEventId != currentEventId){
                        LayoutInflater inflater = (LayoutInflater)context.getSystemService
                                (Context.LAYOUT_INFLATER_SERVICE);
                        View inflatedView = inflater.inflate(R.layout.invitation, null);

                        final View inviteResponseView =  (View) inflatedView.findViewById((int) compareEventId);
                        inviteResponseView.setVisibility(View.GONE);
                    }
                    switch (invitationView.getId()) {
                        case R.id.invitation_single:
                            second.setVisibility(View.VISIBLE);
                            break;

                    }
                }


            });


            return inviteView;
        }

    }
}

但inviteResponseView始终返回null。需要指导来解决这个功能。感谢您的宝贵回应。

编辑: -

我的布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/invitation_single"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="?android:dividerVertical"
    android:dividerPadding="5dp"
    android:showDividers="middle"
    tools:context=".MainActivity">

    <ImageButton
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_action_event" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:clickable="false"
        android:focusable="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/invitation_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="0dp"
            android:paddingTop="3dp"
            android:textColor="@color/black"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/invitation_place"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:paddingRight="0dp"
            android:textColor="@color/black"
            android:textSize="15sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/hidden"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_marginLeft="-270dp"
        android:layout_marginTop="60dp"
        android:layout_weight="1"
        android:clickable="true"
        android:focusable="true"
        android:orientation="horizontal"
        android:paddingTop="1dp"
        android:visibility="gone"
        android:weightSum="3">


        <Button
            android:id="@+id/yesbutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="7dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="Yes"
            android:textColor="@color/black"></Button>

        <Button
            android:id="@+id/nobutton"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="25dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="No"
            android:textColor="@color/black"></Button>

        <Button
            android:id="@+id/buttonmaybe"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="10dp"
            android:layout_weight="1"
            android:background="@color/blue"
            android:text="Maybe"
            android:textColor="@color/black"></Button>

    </LinearLayout>


</LinearLayout>

3 个答案:

答案 0 :(得分:2)

为什么不使用onFocusChangeListener? 首先:

在您的invitationView布局.xml文件中设置(包含是,否,可能按钮的文件),如下所示:

 android:clickable="true"
 android:focusable="true"
 android:focusableInTouchMode="true"

注意: 您还必须对父级布局(活动的布局)执行此操作,因此当用户触摸invitationView外部时,父布局将捕获焦点。

然后,您需要做的是将invitationView.setOnFocusChangeListener设置为Activity内或listAdapater(或recyclerViewAdapter)内的 invitationView ,如果你正在将它用于invitationView(我强烈推荐)。 像这样:

invitationView.setOnFocusChangeListener(new OnFocusChangeListener() {          

        public void onFocusChange(View v, boolean hasFocus) {
            if(!hasFocus)
           {
               inviteResponseView.setVisibility(view.GONE)
           }else
               inviteResponseView.setVisibility(view.VISIBLE)
        }
    });

<强>建议:

建议您使用recyclerView代替片段来完成这项工作,它非常易于使用且内存使用效率很高。现在它支持大多数具有design support library的设备,这些设备已经有一堆教程,比如链接的教程。

答案 1 :(得分:0)

我建议您在项目中使用某些第三方依赖项。

事实上,nhaarman's ListViewAnimations可以很好地做到这一点。下载demo in Play Store并进行检查。

有一个完全可自定义的ListView,可以扩展用户点击并显示更多项目。此外,它支持非常酷的动画。我在我的一个项目中使用它并且它非常好。您甚至可以设置一次可以扩展的最大视图数。

答案 2 :(得分:0)

BaseAdapter添加

private boolean showActionView = false;
private int showActionViewFor;

public void showActionView(boolean show, int position) {
    showActionView = show;
    showActionViewFor = position;
    notifyDataSetChanged();
}

然后在BaseAdapter的{​​{1}}添加

getView()

然后在if(showActionView && position == showActionViewFor) { second.setVisibility(View.VISIBLE); } else { second.setVisibility(View.INVISIBLE); //Don't use View.GONE } 的{​​{1}}中添加

Fragment

的Bam!它应该按你的需要工作。每当您需要隐藏所有操作视图时,只需调用

onCreateView()

更新:

您需要删除

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
         mAdapter.showActionView(true, position);
     }
 });
来自适配器的

额外:要提高mAdapter.showActionView(false, 0); 效果,您应该考虑实施first.setOnClickListener(new View.OnClickListener() {} 模式。