Naar content
Trending apps
  • Inbox by Gmail

  • Maps: Navigatie en OV

  • WhatsApp Messenger

  • Messenger

  • Facebook

Trending games
  • Dr. Mario World

  • Harry Potter: Wizards Unite

  • Breaking Bad: Criminal Elements

  • The Elder Scrolls: Blades

  • Ghostbusters World

Trending smartphones
  • Microsoft Surface Duo

  • OnePlus 7T Pro

  • Nokia 7.2

  • Xiaomi Mi 9T Pro

  • Samsung Galaxy Note 10 Plus

Nieuwste tablets
  • Samsung Galaxy Tab S6

  • Samsung Galaxy Tab A 10.5

  • Samsung Galaxy Tab S4

  • Samsung Galaxy Tab S3 9.7

  • Asus Zenpad 3S 10

wdverste

wdverste

  • Lid sinds 02 september 2011
  • Berichten 8
  • Reputatie 0
  • #1
  • 7 september 2011
  • 14:06

Hallo,

Ik heb een lijst met kleuren gemaakt. dit is een slidingDrawer. Wanneer ik daar een item aantap dan verschijnt er een pop-up menu. In dat pop-up menu zit een TextView. de text daarvan moet gezet worden op de naam van het aangetapte item. Dit lukt met niet kunnen jullie mij misschien helpen, hoe ik dit op moet lossen?

Dit is de Java code van de running application met de slidingDrawer.

public class slidingDrawerExample extends Activity {

Button slideHandleButton;
SlidingDrawer slidingDrawer;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
slideHandleButton = (Button) findViewById(R.id.slideHandleButton);
slidingDrawer = (SlidingDrawer) findViewById(R.id.SlidingDrawer);

slidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {


public void onDrawerOpened() {
slideHandleButton.setBackgroundResource(R.drawable.openarrow);
}
});

slidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener() {

@Override
public void onDrawerClosed() {
slideHandleButton.setBackgroundResource(R.drawable.closearrow);
}
});

ListView lv1 =(ListView)findViewById(R.id.tvList01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setTextFilterEnabled(true);

lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
DemoPopupWindow dw = new DemoPopupWindow(view, position);
dw.showLikePopDownMenu(236,-40);
}
});

}

private static class DemoPopupWindow extends betterPopUpWindow implements OnClickListener {
private int position;

public DemoPopupWindow(View anchor, int position) {
super(anchor);
this.position = position;
}


@Override
protected void onCreate() {
// inflate layout
LayoutInflater inflater =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


ViewGroup root = (ViewGroup) inflater.inflate(R.layout.popup_grid_layout, null);


// setup button events
for(int i = 0, icount = root.getChildCount() ; i < icount ; i++) {
View v = root.getChildAt(i);


if(v instanceof TableRow) {
TableRow row = (TableRow) v;


for(int j = 0, jcount = row.getChildCount() ; j < jcount ; j++) {
View item = row.getChildAt(j);
if(item instanceof Button) {
Button b = (Button) item;
b.setOnClickListener(this);
}
}
}
}


// set the inflated view as what we want to display
this.setContentView(root);
TextView tv = (TextView)root.findViewById(R.id.trtv01);
tv.setText(lv_arr[position]);
}


@Override
public void onClick(View v) {
// we'll just display a simple toast on a button click
Button b = (Button) v;
Toast.makeText(this.anchor.getContext(), b.getText(), Toast.LENGTH_SHORT).show();
this.dismiss();
}
}

private static String lv_arr[]={
"Green", "Yellow", "Red", "Black", "Blue", "Cyan", "GRAY",
"white", "Magenta"
};

}

//Daarnaast nog de code voor het openen van het popup lijstje.

public class betterPopUpWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;


/**
* Create a BetterPopupWindow
*
* @param anchor
* the view that the BetterPopupWindow will be displaying 'from'
*/
public betterPopUpWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());


// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
betterPopUpWindow.this.window.dismiss();
return true;
}
return false;
}
});


this.windowManager = (WindowManager) this.anchor.getContext().getSystemService(Context.WINDOW_SERVICE);
onCreate();
}


/**
* Anything you want to have happen when created. Probably should create a view and setup the event listeners on
* child views.
*/
protected void onCreate() {}


/**
* In case there is stuff to do right before displaying.
*/
protected void onShow() {}


private void preShow() {
if(this.root == null) {
throw new IllegalStateException("setContentView was not called with a view to display.");
}
onShow();


if(this.background == null) {
this.window.setBackgroundDrawable(new BitmapDrawable());
} else {
this.window.setBackgroundDrawable(this.background);
}


// if using PopupWindow#setBackgroundDrawable this is the only values of the width and hight that make it work
// otherwise you need to set the background of the root viewgroup
// and set the popupwindow background to an empty BitmapDrawable
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable(true);
this.window.setFocusable(true);
this.window.setOutsideTouchable(true);


this.window.setContentView(this.root);
}


public void setBackgroundDrawable(Drawable background) {
this.background = background;
}


/**
* Sets the content view. Probably should be called from {@link onCreate}
*
* @param root
* the view the popup will display
*/
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}


/**
* Will inflate and set the view from a resource id
*
* @param layoutResID
*/
public void setContentView(int layoutResID) {
LayoutInflater inflator =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}


/**
* If you want to do anything when {@link dismiss} is called
*
* @param listener
*/
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}


/**
* Displays like a popdown menu from the anchor view
*/
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}


/**
* Displays like a popdown menu from the anchor view.
*
* @param xOffset
* offset in X direction
* @param yOffset
* offset in Y direction
*/
public void showLikePopDownMenu(int xOffset, int yOffset) {
this.preShow();


this.window.setAnimationStyle(R.style.Animations_PopDownMenu);


this.window.showAsDropDown(this.anchor, xOffset, yOffset);
}


/**
* Displays like a QuickAction from the anchor view.
*/
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}


/**
* Displays like a QuickAction from the anchor view.
*
* @param xOffset
* offset in the X direction
* @param yOffset
* offset in the Y direction
*/
public void showLikeQuickAction(int xOffset, int yOffset) {
this.preShow();


this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);


int[] location = new int[2];
this.anchor.getLocationOnScreen(location);


Rect anchorRect =
new Rect(location[0], location[1], location[0] + this.anchor.getWidth(), location[1]
+ this.anchor.getHeight());


this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);


int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();


int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
//int screenHeight = this.windowManager.getDefaultDisplay().getHeight();


int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
int yPos = anchorRect.top - rootHeight + yOffset;


// display on bottom
if(rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}


this.window.showAtLocation(this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}


public void dismiss() {
this.window.dismiss();
}


public void onClick(View v) {
// TODO Auto-generated method stub

}
}

Bewerkt (7 september 2011 14:06)
wdverste

wdverste

  • Lid sinds 02 september 2011
  • Berichten 8
  • Reputatie 0
  • #2
  • 7 september 2011
  • 14:06

En dit is de .xml's

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:gravity="left"
android:background="@drawable/myresouce">

<SlidingDrawer android:layout_width="fill_parent"
android:layout_gravity="left"
android:id="@+id/SlidingDrawer"
android:orientation="vertical"
android:handle="@+id/slideHandleButton"
android:content="@+id/contentLayout"
android:padding="10dip"
android:layout_height="wrap_content"
android:layout_weight="3">

<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/slideHandleButton"
android:background="@drawable/closearrow">
</Button>
<LinearLayout android:layout_width="wrap_content"
android:id="@+id/contentLayout"
android:orientation="vertical"
android:gravity="center|top"
android:padding="10dip"
android:background="#C0C0C0"
android:layout_height="wrap_content">

<Button android:id="@+id/terugBt"
android:layout_height = "wrap_content"
android:layout_width = "wrap_content"
android:text="Terug">
</Button>

<ListView
android:id="@+id/tvList01"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent">
</ListView>
</LinearLayout>
</SlidingDrawer>

<TextView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/editText1"
android:padding="5pt"
android:textColor="#FFFFFF"
android:layout_weight="1">
</TextView>
</LinearLayout>


<!-- En dan komt nu de .xml voor de pop-up lijst -->

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:drawable/alert_light_frame"
>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/trtv01"
android:text="Test"
android:textColor="#000000">
</TextView>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/one"
android:text="One"
android:minWidth="80dp"
android:minHeight="80dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/two"
android:text="Two"
android:minWidth="80dp"
android:minHeight="80dp"
/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/four"
android:text="Three"
android:minHeight="80dp"
/>
<Button
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/five"
android:text="Four"
android:minHeight="80dp"
/>
</TableRow>
</TableLayout>

Bewerkt (7 september 2011 14:06)
redbulletnl

redbulletnl

  • Lid sinds 02 september 2011
  • Berichten 2991
  • Reputatie 60
  • #3
  • 7 september 2011
  • 14:15

die items die je aan kunt klikken zijn buttons? (sorry voor de domme vraag)

Bewerkt (7 september 2011 14:15)
wdverste

wdverste

  • Lid sinds 02 september 2011
  • Berichten 8
  • Reputatie 0
  • #4
  • 7 september 2011
  • 14:17

Dat is een ListView kweet nie of het buttons zijn..

Bewerkt (7 september 2011 14:17)
redbulletnl

redbulletnl

  • Lid sinds 02 september 2011
  • Berichten 2991
  • Reputatie 60
  • #5
  • 7 september 2011
  • 14:19

string aangeklikteTekst;

aangeklikteTekst = (String) ((TextView) view).getText();


zou zoiets helpen?

Bewerkt (7 september 2011 14:19)
wdverste

wdverste

  • Lid sinds 02 september 2011
  • Berichten 8
  • Reputatie 0
  • #6
  • 7 september 2011
  • 14:21

Nee..
Hij krijgt iedere keer dezelfde waarde in de variabele position in de DemoPopUp window class

Bewerkt (7 september 2011 14:21)
IvoJanssen

IvoJanssen

  • Lid sinds 25 februari 2010
  • Berichten 275
  • Reputatie 0
  • #7
  • 7 september 2011
  • 16:30

Schijnbaar komt de onCreate functie voor de constructor, daarom is position daar nog altijd 0.
Een simpele oplossing:

                DemoPopupWindow dw = new DemoPopupWindow(view, position);
dw.setText(); // extra om de textview te setten[INDENT=2]dw.showLikePopDownMenu(236,-40);[/INDENT]

In DemoPopup extra functie:

            public void setText() {
LayoutInflater inflater =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);


ViewGroup root = (ViewGroup) inflater.inflate(R.layout.popup, null);

TextView tv = (TextView)root.findViewById(R.id.trtv01);
tv.setText(lv_arr[position]);
this.setContentView(root);
}

Succes ermee verder!

Bewerkt (7 september 2011 16:30)

Reageer

Om te reageren, dien je te zijn ingelogd. Druk op de onderstaande knop om in te loggen of maak een nieuwe account aan.

Inloggen Registreren