Android Master-Detail应用程序

时间:2016-05-11 01:08:27

标签: android xml android-layout android-fragments android-volley

我正在努力实现以下目标 -
How my Application will look


“将通过齐射获取主要细节的两个窗格流和JSON中的数据源。”


我的斗争是,当主面板上发生clickEvent()时,我不知道如何传输数据。 我遵循了一些教程,如
http://mobileappdocs.com/android.html
http://www.techotopia.com/index.php/An_Android_Master/Detail_Flow_Tutorial
我仍然没有得到那个部分。

是不是因为我使用Android Studio中的默认主从细节布局,其中只有右侧(细节)面板是Fragment而左侧不是。 两个面板都需要是片段吗?

我的代码和进展:我已经完成了布局,我正在努力处理后面的Java代码
guest_list.xml

<LinearLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="none"
tools:context="awesome.pyie.com.finalapp.GuestListActivity">

<!--
This layout is a two-pane layout for the Guests
master/detail flow.

-->

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@color/AppDarkGrey">

    <SearchView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:id="@+id/searchView"
    android:background="@color/AppLightGrey"
    android:iconifiedByDefault="false"
    android:queryHint="Search">
    </SearchView>

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/guest_list"
    android:name="awesome.pyie.com.finalapp.GuestListFragment"
    android:layout_width="308dp"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    tools:context="awesome.pyie.com.finalapp.GuestListActivity"
    tools:listitem="@layout/guest_list_content" />


</LinearLayout>

<FrameLayout
    android:background="@color/AppLightGrey"
    android:id="@+id/guest_detail_container"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="3">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add_mid_one"
        android:id="@+id/imageView"
        android:layout_gravity="center"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="onClick"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Add Guest"
        android:textColor="@color/AppWhite"
        android:id="@+id/textView"
        android:layout_gravity="center"
        android:layout_below="@+id/imageView"
        android:layout_centerHorizontal="true" />
    </RelativeLayout>

</FrameLayout>



guest_list_content.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/display1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:background="@color/AppDarkGrey"> <!--Slave Layout-->

<de.hdodenhof.circleimageview.CircleImageView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/profile_image"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@drawable/logo"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    app:civ_border_width="2dp"
    app:civ_border_color="#83848A"/>

<TextView
    android:id="@+id/id"
    android:textColor="@color/AppWhite"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:layout_marginRight="10dp"
    android:layout_alignTop="@+id/profile_image"
    android:layout_alignStart="@+id/content" />

<TextView
    android:id="@+id/content"
    android:textColor="@color/AppBlue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:layout_marginLeft="@dimen/text_margin"
    android:layout_marginRight="@dimen/text_margin"
    android:layout_alignBottom="@+id/profile_image"
    android:layout_toEndOf="@+id/profile_image"
    android:layout_marginStart="26dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/AppTime"
    android:text="5:58pm"
    android:layout_marginRight="10dp"
    android:layout_centerVertical="true"
    android:layout_alignParentEnd="true" />

<View android:background="@color/AppLightGrey"   android:layout_width="fill_parent" android:layout_height="1dip" />


MyListActivity (包含弹出对话框请忽略该部分)

package awesome.pyie.com.finalapp;
import android.content.Context;  
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.SearchView;
import android.util.DisplayMetrics;
import android.view.View.OnClickListener;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Dialog;
import android.widget.Toast;
import org.w3c.dom.Text;
import awesome.pyie.com.finalapp.dummy.DummyContent;
import java.util.List;


public class GuestListActivity extends AppCompatActivity {

private boolean mTwoPane;
final Context context = this;

//API END POINT
public static final String JSON_URL = "*******";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_guest_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    //Toolbar title Removed
    getSupportActionBar().setDisplayShowTitleEnabled(false);

         toolbar.findViewById(R.id.textView2).setVisibility(View.INVISIBLE);

    toolbar.findViewById(R.id.addTop).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setContentView(R.layout.add_user_dialog);
            dialog.show();

            //Calculate Device Width and height
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
            float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

            Window window = dialog.getWindow();
            window.setLayout((int)(dpWidth), (int)((dpHeight)+50));
               window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

            TextView cancelText = (TextView) dialog.findViewById(R.id.textView2);

            cancelText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
    });


    View recyclerView = findViewById(R.id.guest_list);
    assert recyclerView != null;
    setupRecyclerView((RecyclerView) recyclerView);

    if (findViewById(R.id.guest_detail_container) != null) {
        // The detail container view will be present only in the
        // large-screen layouts (res/values-w900dp).
        // If this view is present, then the
        // activity should be in two-pane mode.
        mTwoPane = true;
        toolbar.findViewById(R.id.textView2).setVisibility(View.VISIBLE);
    }


}

private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
    recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}

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

    private final List<DummyContent.DummyItem> mValues;

    public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
        mValues = items;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mItem = mValues.get(position);
        holder.mIdView.setText(mValues.get(position).id);
        holder.mContentView.setText(mValues.get(position).content);

        final ImageView imageView =(ImageView)findViewById(R.id.imageView);
        final TextView textView = (TextView)findViewById(R.id.textView);
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTwoPane) {
                    //Code Here
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.GONE);

                    //Ends Here
                    Bundle arguments = new Bundle();
                    arguments.putString(GuestDetailFragment.ARG_ITEM_ID, holder.mItem.id);
                    GuestDetailFragment fragment = new GuestDetailFragment();
                    fragment.setArguments(arguments);
                    getSupportFragmentManager().beginTransaction()
                            .replace(R.id.guest_detail_container, fragment)
                            .commit();
                } else {
                    Context context = v.getContext();
                    Intent intent = new Intent(context, GuestDetailActivity.class);
                    intent.putExtra(GuestDetailFragment.ARG_ITEM_ID, holder.mItem.id);
                    context.startActivity(intent);
                }
            }
        });


        imageView.setOnClickListener(new View.OnClickListener() {
            //Start new list activity
            @Override
            public void onClick(View v) {
                final Dialog dialog = new Dialog(context);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.add_user_dialog);
                dialog.show();

                //Calculate Device Width and height
                DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
                float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
                float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

                Window window = dialog.getWindow();
                window.setLayout((int)(dpWidth), (int)((dpHeight)+50));

           window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);

                TextView cancelText = (TextView) dialog.findViewById(R.id.textView2);

                cancelText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

            }
        });        //Add Button Function - will display the add page


    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final View mView;
        public final TextView mIdView;
        public final TextView mContentView;
        public DummyContent.DummyItem mItem;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mIdView = (TextView) view.findViewById(R.id.id);
            mContentView = (TextView) view.findViewById(R.id.content);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mContentView.getText() + "'";
        }
    }
}


如果你们想让我发布任何代码,请告诉我。提前致谢!

0 个答案:

没有答案