2015. 5. 22. 10:19

탐색기 왼쪽처럼 나오는 소스

package com.newkie.testactivity;


import java.util.ArrayList;


import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.MotionEvent;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewTreeObserver;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.ScrollView;

import android.widget.TextView;

import android.widget.Toast;


import com.newkie.testactivity.Product.SubCategory;

import com.newkie.testactivity.Product.SubCategory.SubSubCategory;

import com.newkie.testactivity.Product.SubCategory.SubSubCategory.ItemList;


public class MainActivity extends Activity

{

LinearLayout mLinearListView;

ScrollView svMain;


private Context mContext = this;

private boolean isFirstViewClick=false;

private boolean isSecondViewClick=false;

private boolean isThirdViewClick=false;

ArrayList<Product> pProductArrayList=new ArrayList<Product>();

ArrayList<SubCategory> pSubItemArrayList=new ArrayList<SubCategory>();

ArrayList<SubCategory> pSubItemArrayList2=new ArrayList<SubCategory>();

ArrayList<SubSubCategory> pSubSubItemArrayList=new ArrayList<SubSubCategory>();

ArrayList<SubSubCategory> pSubSubItemArrayList2=new ArrayList<SubSubCategory>();

ArrayList<SubSubCategory> pSubSubItemArrayList3=new ArrayList<SubSubCategory>();


@Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

SetTempValue();

initValue();

}

@SuppressLint("InflateParams")

private void initValue()

{

mLinearListView = (LinearLayout) findViewById(R.id.llMain);

svMain = (ScrollView)findViewById(R.id.svMain);


SetTempValue();


//Adding Items into List


//First Level

for (int i = 0; i < pProductArrayList.size(); i++)

{

LayoutInflater inflater = null;

inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mLinearView = inflater.inflate(R.layout.row_first, null);


final TextView mProductName = (TextView) mLinearView.findViewById(R.id.textViewName);

final LinearLayout mLinearFirstArrow=(LinearLayout)mLinearView.findViewById(R.id.linearFirst);

final ImageView mImageArrowFirst=(ImageView)mLinearView.findViewById(R.id.imageFirstArrow);

final LinearLayout mLinearScrollSecond=(LinearLayout)mLinearView.findViewById(R.id.linear_scroll);


if(isFirstViewClick==false)

{

mLinearScrollSecond.setVisibility(View.GONE);

mImageArrowFirst.setBackgroundResource(R.drawable.folder_close);

}

else

{

mLinearScrollSecond.setVisibility(View.VISIBLE);

mImageArrowFirst.setBackgroundResource(R.drawable.folder_open);

}


mLinearFirstArrow.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

if(mLinearScrollSecond.getVisibility() == View.GONE)

{

isFirstViewClick=true;

mImageArrowFirst.setBackgroundResource(R.drawable.folder_open);

mLinearScrollSecond.setVisibility(View.VISIBLE);


}

else

{

isFirstViewClick=false;

mImageArrowFirst.setBackgroundResource(R.drawable.folder_close);

mLinearScrollSecond.setVisibility(View.GONE);

}

}

});


final String name = pProductArrayList.get(i).getpName();

mProductName.setText(name);


//Second Level

for (int j = 0; j < pProductArrayList.get(i).getmSubCategoryList().size(); j++)

{

LayoutInflater inflater2 = null;

inflater2 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mLinearView2 = inflater2.inflate(R.layout.row_middle, null);


TextView mSubItemName = (TextView) mLinearView2.findViewById(R.id.textViewName);

final LinearLayout mLinearSecondArrow=(LinearLayout)mLinearView2.findViewById(R.id.linearMiddle);

final ImageView mImageArrowSecond=(ImageView)mLinearView2.findViewById(R.id.imageMiddleArrow);

final LinearLayout mLinearScrollThird=(LinearLayout)mLinearView2.findViewById(R.id.linear_scroll_middle);


if(isSecondViewClick==false)

{

mLinearScrollThird.setVisibility(View.GONE);

mImageArrowSecond.setBackgroundResource(R.drawable.folder_close);

}

else

{

mLinearScrollThird.setVisibility(View.VISIBLE);

mImageArrowSecond.setBackgroundResource(R.drawable.folder_open);

}


mLinearSecondArrow.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

if(mLinearScrollThird.getVisibility() == View.GONE)

{

isSecondViewClick=true;

mImageArrowSecond.setBackgroundResource(R.drawable.folder_open);

mLinearScrollThird.setVisibility(View.VISIBLE);

}

else

{

isSecondViewClick=false;

mImageArrowSecond.setBackgroundResource(R.drawable.folder_close);

mLinearScrollThird.setVisibility(View.GONE);

}

}

});



final String catName = pProductArrayList.get(i).getmSubCategoryList().get(j).getpSubCatName();

mSubItemName.setText(catName);


//Third Level

for (int k = 0; k < pProductArrayList.get(i).getmSubCategoryList().get(j).getmSubSubCategoryList().size(); k++)

{

LayoutInflater inflater3 = null;

inflater3 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mLinearView3 = inflater3.inflate(R.layout.row_middle, null);


TextView mSubSubItemName = (TextView) mLinearView3.findViewById(R.id.textViewName);

final LinearLayout mLinearThirdArrow=(LinearLayout)mLinearView3.findViewById(R.id.linearMiddle);

final ImageView mImageArrowThird=(ImageView)mLinearView3.findViewById(R.id.imageMiddleArrow);

final LinearLayout mLinearScrollFourth=(LinearLayout)mLinearView3.findViewById(R.id.linear_scroll_middle);


if(isThirdViewClick==false)

{

mLinearScrollFourth.setVisibility(View.GONE);

mImageArrowThird.setBackgroundResource(R.drawable.folder_close);

}

else

{

mLinearScrollFourth.setVisibility(View.VISIBLE);

mImageArrowThird.setBackgroundResource(R.drawable.folder_open);

}


mLinearThirdArrow.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

if(mLinearScrollFourth.getVisibility() == View.GONE)

{

isThirdViewClick=true;

mImageArrowThird.setBackgroundResource(R.drawable.folder_open);

mLinearScrollFourth.setVisibility(View.VISIBLE);

}

else

{

isThirdViewClick=false;

mImageArrowThird.setBackgroundResource(R.drawable.folder_close);

mLinearScrollFourth.setVisibility(View.GONE);

}

}

});


final String catCatName = pProductArrayList.get(i).getmSubCategoryList().get(j).getmSubSubCategoryList().get(k).getpSubSubCatName();

mSubSubItemName.setText(catCatName);


//Fourth Level

for (int l = 0; l < pProductArrayList.get(i).getmSubCategoryList().get(j).getmSubSubCategoryList().get(k).getmItemListArray().size(); l++)

{

LayoutInflater inflater4 = null;

inflater4 = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View mLinearView4 = inflater4.inflate(R.layout.row_last, null);


TextView mItemName = (TextView) mLinearView4.findViewById(R.id.textViewItemName);

TextView mItemPrice = (TextView) mLinearView4.findViewById(R.id.textViewItemPrice);

final String itemName = pProductArrayList.get(i).getmSubCategoryList().get(j).getmSubSubCategoryList().get(k).getmItemListArray().get(l).getItemName();

final String itemFname = pProductArrayList.get(i).getmSubCategoryList().get(j).getmSubSubCategoryList().get(k).getmItemListArray().get(l).getItemPrice();

mItemName.setText(itemName);

mItemPrice.setText(itemFname);


mLinearView4.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

Toast.makeText(mContext, itemName+"입니다.", Toast.LENGTH_SHORT).show();

}

});

mLinearScrollFourth.addView(mLinearView4);

}

mLinearScrollThird.addView(mLinearView3);

}

mLinearScrollSecond.addView(mLinearView2);

}

mLinearListView.addView(mLinearView);

}


svMain.setOnTouchListener(new View.OnTouchListener()

{

@Override

public boolean onTouch(View v, MotionEvent event)

{

return false;

}

});

}


private void SetTempValue()

{

ArrayList<ItemList> mItemListArray=new ArrayList<ItemList>();

mItemListArray.add(new ItemList("나", "1"));

mItemListArray.add(new ItemList("는", "2"));


ArrayList<ItemList> mItemListArray2=new ArrayList<ItemList>();

mItemListArray2.add(new ItemList("말", "3"));

mItemListArray2.add(new ItemList("단", "4"));


ArrayList<ItemList> mItemListArray3=new ArrayList<ItemList>();

mItemListArray3.add(new ItemList("입", "5"));

mItemListArray3.add(new ItemList("니", "6"));


ArrayList<ItemList> mItemListArray4=new ArrayList<ItemList>();

mItemListArray4.add(new ItemList("다", "7"));

mItemListArray4.add(new ItemList("!", "8"));


ArrayList<ItemList> mItemListArray5=new ArrayList<ItemList>();

mItemListArray5.add(new ItemList("말단", "9"));

mItemListArray5.add(new ItemList("말단", "9"));


pSubItemArrayList=new ArrayList<SubCategory>();

pSubItemArrayList2=new ArrayList<SubCategory>();

pSubSubItemArrayList=new ArrayList<SubSubCategory>();

pSubSubItemArrayList2=new ArrayList<SubSubCategory>();

pSubSubItemArrayList3=new ArrayList<SubSubCategory>();


pSubSubItemArrayList.add(new SubSubCategory("1-1-1", false,  mItemListArray));

pSubSubItemArrayList.add(new SubSubCategory("1-1-2", false,  mItemListArray2));

pSubSubItemArrayList2.add(new SubSubCategory("1-2-1", false,  mItemListArray3));

pSubSubItemArrayList2.add(new SubSubCategory("1-2-2", false,  mItemListArray4));

pSubSubItemArrayList3.add(new SubSubCategory("2-1-1", false,  mItemListArray5));

pSubSubItemArrayList3.add(new SubSubCategory("2-1-2", false,  mItemListArray5));


pSubItemArrayList.add(new SubCategory("1-1", false, pSubSubItemArrayList));

pSubItemArrayList.add(new SubCategory("1-2", false, pSubSubItemArrayList2));

pSubItemArrayList2.add(new SubCategory("2-1", false, pSubSubItemArrayList3));


pProductArrayList=new ArrayList<Product>();

pProductArrayList.add(new Product("1", false, pSubItemArrayList));

pProductArrayList.add(new Product("2", false, pSubItemArrayList2));

}


}





Product.java



// first level item

public class Product

{

private String pName;

private boolean isFirstClicked = false;


private ArrayList<SubCategory> mSubCategoryList;


public Product(String pName, boolean isFirstClicked, ArrayList<SubCategory> mSubCategoryList)

{

super();

this.pName = pName;

this.isFirstClicked = isFirstClicked;

this.mSubCategoryList = mSubCategoryList;

}


public String getpName()

{

return pName;

}


public void setpName(String pName)

{

this.pName = pName;

}


public boolean isFirstClicked()

{

return isFirstClicked;

}


public void setFirstClicked(boolean isFirstClicked)

{

this.isFirstClicked = isFirstClicked;

}


public ArrayList<SubCategory> getmSubCategoryList()

{

return mSubCategoryList;

}


public void setmSubCategoryList(ArrayList<SubCategory> mSubCategoryList)

{

this.mSubCategoryList = mSubCategoryList;

}



// second level item

public static class SubCategory

{

private String pSubCatName;

private boolean isSecondClicked = false;


private ArrayList<SubSubCategory> mSubSubCategoryList;


public SubCategory(String pSubCatName, boolean isSecondClicked,

ArrayList<SubSubCategory> mSubSubCategoryList)

{

super();

this.pSubCatName = pSubCatName;

this.isSecondClicked = isSecondClicked;

this.mSubSubCategoryList = mSubSubCategoryList;

}


public String getpSubCatName()

{

return pSubCatName;

}


public void setpSubCatName(String pSubCatName)

{

this.pSubCatName = pSubCatName;

}


public boolean isSecondClicked() {

return isSecondClicked;

}


public void setSecondClicked(boolean isSecondClicked) {

this.isSecondClicked = isSecondClicked;

}


public ArrayList<SubSubCategory> getmSubSubCategoryList()

{

return mSubSubCategoryList;

}


public void setmItemListArray(ArrayList<SubSubCategory> mSubSubCategoryList)

{

this.mSubSubCategoryList = mSubSubCategoryList;

}


// third level item

public static class SubSubCategory

{

private String pSubSubCatName;

private boolean isThirdClicked = false;

private ArrayList<ItemList> mItemListArray;


public SubSubCategory(String pSubSubCatName, boolean isThirdClicked,

ArrayList<ItemList> mItemListArray)

{

super();

this.pSubSubCatName = pSubSubCatName;

this.isThirdClicked = isThirdClicked;

this.mItemListArray = mItemListArray;

}


public String getpSubSubCatName()

{

return pSubSubCatName;

}


public void setpSubSubCatName(String pSubSubCatName)

{

this.pSubSubCatName = pSubSubCatName;

}


public boolean isThirdClicked() {

return isThirdClicked;

}


public void setThirdClicked(boolean isThirdClicked) {

this.isThirdClicked = isThirdClicked;

}


public ArrayList<ItemList> getmItemListArray()

{

return mItemListArray;

}


public void setmItemListArray(ArrayList<ItemList> mItemListArray)

{

this.mItemListArray = mItemListArray;

}



// 4th level item


public static class ItemList

{


private String itemName;

private String itemPrice;


public ItemList(String itemName, String itemPrice)

{

super();

this.itemName = itemName;

this.itemPrice = itemPrice;

}


public String getItemName()

{

return itemName;

}


public void setItemName(String itemName)

{

this.itemName = itemName;

}


public String getItemPrice()

{

return itemPrice;

}


public void setItemPrice(String itemPrice)

{

this.itemPrice = itemPrice;

}


}


}


}


}



activity_main.xml


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/svMain"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical"

    tools:context="${relativePackage}.${activityClass}" >


    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical" >


        <LinearLayout

            android:id="@+id/llMain"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="vertical" />

    </LinearLayout>


</ScrollView>



row_first.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="match_parent"

    android:background="@android:color/white"

    android:orientation="vertical" >


    <LinearLayout

        android:id="@+id/linearFirst"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_vertical"

        android:minHeight="40dp" >


        <ImageView

            android:id="@+id/imageFirstArrow"

            android:layout_width="20dp"

            android:layout_height="20dp"

            android:contentDescription="@string/def_folder" />


        <TextView

            android:id="@+id/textViewName"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:paddingEnd="0dp"

            android:paddingLeft="5dp"

            android:paddingRight="0dp"

            android:paddingStart="5dp"

            android:text="@string/def_title"

            android:textColor="#000000"

            android:textSize="20sp" />

        <!--

        <TextView

            android:id="@+id/textViewName"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:text="TextView"

            android:textColor="@android:color/white"

            android:textSize="22sp" />


        <ImageView

            android:id="@+id/imageFirstArrow"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentRight="true"

            android:layout_marginRight="5dp" />

        -->

    </LinearLayout>


    <LinearLayout

        android:id="@+id/linear_scroll"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10dp"

        android:layout_marginStart="10dp"

        android:background="@android:color/white"

        android:orientation="vertical" />


    <View

        android:layout_width="match_parent"

        android:layout_height="1dp"

        android:background="@android:color/white" />


</LinearLayout>


row_middle.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="match_parent"

    android:background="@android:color/white"

    android:orientation="vertical" >


    <LinearLayout

        android:id="@+id/linearMiddle"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center_vertical"

        android:minHeight="40dp" >


        <ImageView

            android:id="@+id/imageMiddleArrow"

            android:layout_width="20dp"

            android:layout_height="20dp"

            android:contentDescription="@string/def_folder" />


        <TextView

            android:id="@+id/textViewName"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:paddingEnd="0dp"

            android:paddingLeft="5dp"

            android:paddingRight="0dp"

            android:paddingStart="5dp"

            android:text="@string/def_title"

            android:textColor="#000000"

            android:textSize="20sp" />

        <!--

        <TextView

            android:id="@+id/textViewName"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentLeft="true"

            android:layout_marginLeft="10dp"

            android:text="TextView"

            android:textColor="@android:color/white"

            android:textSize="22sp" />


        <ImageView

            android:id="@+id/imageSecondArrow"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_alignParentRight="true"

            android:layout_marginRight="5dp" />

        -->

    </LinearLayout>


    <LinearLayout

        android:id="@+id/linear_scroll_middle"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10dp"

        android:layout_marginStart="10dp"

        android:background="@android:color/white"

        android:orientation="vertical" />


    <View

        android:layout_width="match_parent"

        android:layout_height="1dp"

        android:background="@android:color/white" />


</LinearLayout>


row_last.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="match_parent"
    android:background="@android:color/white"
    android:gravity="center_vertical"
    android:minHeight="40dp"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageFourthArrow"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:contentDescription="@string/def_folder"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textViewItemName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingEnd="0dp"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingStart="5dp"
        android:text="@string/def_name"
        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textViewItemPrice"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingEnd="0dp"
        android:paddingLeft="5dp"
        android:paddingRight="0dp"
        android:paddingStart="5dp"
        android:text="@string/def_fname"
        android:textColor="#000000"
        android:textSize="20sp" />

</LinearLayout>


Expandable ListView가 너무나도 안되서


리스트뷰 아니고 레이아웃으로 구현됨....

'안드로이드' 카테고리의 다른 글

문자열로 클래스 만들기  (0) 2015.05.22
xml안에 xml넣기  (1) 2015.05.22
Strings 에서 parameter 넣기  (0) 2015.05.11
숏컷(바로가기) 생성하기  (0) 2014.12.22
파일 탐색기  (0) 2014.10.02
Posted by newkie